![]() When your browser has nothing to execute it takes an event from queue and executes them one by one. ![]() Whenever an event get triggered in js, all of these events (like click etc. There is a event queue implemented in browser. ![]() To understand that you have to know about event queue in javascript. If you run the the above code you will see that it alerts before setInterval and then after setInterval finally it alerts I am setInterval 5 times after 1sec (1000ms) because the setTimeout clear the timer after 5 seconds or else every 1 second you will get alert I am setInterval Infinitely. before getting cleared by below timeout.Īlert("after setInterval") //called secondĬlearInterval(tid) //clear above interval after 5 seconds called 5 times each time after one second SetInterval alert("before setInterval") //called first What you can notice from the example is that the setTimeout(.) is asynchronous which means it doesn't wait for the timer to get elapsed before going to next statement i.e alert("after setTimeout") If you run the the above code you will see that it alerts before setTimeout and then after setTimeout finally it alerts I am setTimeout after 1sec (1000ms) SetTimeout executes only once after the delay whereas setInterval keeps on calling the callback function after every delay milisecs.īoth these methods returns an integer identifier that can be used to clear them before the timer expires.ĬlearTimeout and clearInterval both these methods take an integer identifier returned from above functions setTimeout and setInterval They take a call back function and delay time as parameter. There are two (mostly used) types of timer function in javascript setTimeout and setInterval ( other)īoth these methods have same signature.
0 Comments
Leave a Reply. |