Introduction

This article explains JavaScript native timers. We will see how to work with these timers. This article covers the following timer methods:
  1. Set Interval
  2. Clear Interval
  3. Set Timeout
  4. Clear Timeout
Of the above four methods, only two are used for setting the timers and the other two are used for canceling their effect.
How timers work?
To understand how timers work we need to understand how JavaScript works.
Just look at the following image to get a better understanding:

Using Set Timeout

Set timeout is used for executing a specific code snippet or function after a specific amount of delay.
Syntax
  1. var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]); var timeoutID = window.setTimeout(code, delay);
An important note is that this function assures a minimum amount of delay of the amount given in the parameter but the actual delay may be greater depending on the status of the execution queue.

Using Clear Timeout

Clear timeout is used for canceling the effect of the "setTimeout()" method. This method cancels the scheduled call of the snippet scheduled by "setTimeout()".
Syntax
  1. window.clearTimeout(timeoutID)
timeoutID is the same id generated by "setTimeout()".
This id helps in canceling the specific timeout method call.

Using Set Interval

This function precedes "setTimeout()" to provide a repeated call of a specific code snippet after some interval.
Syntax
  1. var intervalID = window.setInterval(func, delay[, param1, param2, ...]); var intervalID = window.setInterval(code, delay);
As with "setTimeout()", "setInterval()" assures a minimum delay but the actual delay depends on the execution queue.
The minimum value of delay that the user can use is 4 milliseconds.
In an execution queue it's not possible for two interval calls to be adjacent. Until a last interval call in the queue is processed all the remaining interval calls are dropped from being queued in the execution queue.

Using Clear Interval

Clear interval is used for canceling the effect of "setInterval()". This method stops the "setInterval()" timer but it has no effect on calls that are already in the execution queue.
Syntax
  1. window.clearInterval(intervalID)
intervalID is an identifier of the repeated action to be canceled. It is generated by the "setInterval()" method.
Examples
Set Timeout with Clear Timeout
  1. $(document).ready(function(){
  2. var doFun1=function(){
  3. console.log("doFun1 is done!");
  4. clearTimeout(intervalID2);
  5. console.log("doFun2 is canceled!");
  6. };
  7. var doFun2=function(){
  8. console.log("doFun2 is done!");
  9. };
  10. var intervalID1 = window.setTimeout(doFun1, 1000);
  11. var intervalID2 = window.setTimeout(doFun2, 1200);
  12. });
Output
Set Interval with Clear Interval
  1. $(document).ready(function(){
  2. var fun=2;
  3. var doFun1=function(){
  4. console.log("fun is increased by "+fun+++ " times");
  5. if(fun>7){
  6. clearInterval(intervalID1);
  7. console.log("No more fun please!");
  8. }
  9. };
  10. var doFun2=function(){
  11. console.log("doFun2 is done!");
  12. };
  13. var intervalID1 = window.setInterval(doFun1, 1000);
  14. });
Output

Summary

That's all for this article. The concept is a bit tricky but it must be understood for every JavaScripter. I hope you have found this article useful and enjoyed reading it. In case of any questions feel free to ask your questions in the comment section.