What is the difference between setinterval and settimeout
Posted By : Himanshu Khurana | 30-Dec-2017
SetTimeout and setInterval are two functions of a JavaScript timer. They can be used to create functions that run once or unlimited. That is, if you want a function to run once using .setTimeout (), use .setInterval () if you want to execute a function indefinitely.
setTimeout function is a native JavaScript function, this function calls the function or executes the code fragment after the specified delay (in milliseconds). For example, a pop-up window appears if you want the pop-up window to appear after the visitor has browsed the page for some time, or to eliminate the effect of the displacement of the item (user accidental deletion).
The JavaScript method setinterval () evaluates an expression or calls a function after each specific number of milliseconds. If you do not cancel by calling the clearinterval () method, continue.
You can also time these two functions in milliseconds. This means that milliseconds are the time they will run later.
You can use the "setTimeout () and setInterval ()" functions in all your JavaScript libraries and functions: jQuery, Angular, React, Vue, Knockoutjs, AngularJS
Lets take an example
1. setTimeout show an alert just one time after 5 seconds.
I will use .setTimeout() and the code will be:
setTimeout(function(){ alert("Hello"); }, 5000);
2. setInterval show an alert unlimited times after every 5 seconds.
I will use .setInterval() and the code will be:
setInterval(function(){ alert("Hello"); }, 5000);
Lots of uses of these 2 function like:
1. Animation
2. Slide
3. etc
I Hope this blog is
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Himanshu Khurana
Himanshu is an experienced Frontend developer with experience and capabilities to build compelling UI's for Web and Mobile Applications. Himanshu likes playing cricket and listening music.