Introduction

In this article, we will look at how the JavaScript timer functions - settimeout, setinterval, and Cleartimeout -- are used to schedule and cancel timer-based Callbacks, with a simple example of a stopwatch program.
Brief of settimeout
Below is the syntax
var id = setTimeout(fn, delay)
  1. First Parameter(fn) => Any function name
  2. Second Parameter(delay) => specifies the no. of milliseconds to wait before it starts the execution of the given function
Note

The settimeout function returns a unique ID which can be used to cancel the timer at any time using Cleartimeout which is discussed below.
Example- Implemented stopwatch functionality using settimeout
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. window.onload = function() {
  5. var startbutton = document.getElementById("btnstart");
  6. var stopbutton = document.getElementById("btnstop");
  7. var clearbutton = document.getElementById("btnclear");
  8. var seconds = 0,
  9. minutes = 0,
  10. hours = 0,
  11. currenttimervalue = 0;
  12. function CalculateTimerPartsAndDisplay() {
  13. //increment logic
  14. seconds++;
  15. if (seconds >= 60) {
  16. seconds = 0;
  17. minutes++;
  18. if (minutes >= 60) {
  19. minutes = 0;
  20. hours++;
  21. } //end of if(minutes>= 60)
  22. } //end of if(seconds>= 60)
  23. //display
  24. document.getElementById("res").innerHTML = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds ? (seconds > 9 ? seconds : "0" + seconds) : "00")
  25. //recursive
  26. timerlogicintiator();
  27. } //end of function CalculateTimerPartsAndDisplay()
  28. function timerlogicintiator() {
  29. currenttimervalue = setTimeout(CalculateTimerPartsAndDisplay, 1000);
  30. } //end of function timerlogicintiator()
  31. //intial call of timer logic
  32. timerlogicintiator();
  33. //call timer logic when startbutton clicks
  34. startbutton.onclick = timerlogicintiator;
  35. //stop the watch by using clearTimeout function
  36. stopbutton.onclick = function() {
  37. clearTimeout(currenttimervalue);
  38. }
  39. clearbutton.onclick = function() {
  40. document.getElementById("res").innerHTML = "00:00:00";
  41. seconds = 0, minutes = 0, hours = 0;
  42. }
  43. } //end of onload function
  44. </script>
  45. </head>
  46. <body>
  47. <h1 id="res">00:00:00
  48. </h1>
  49. <input type="button" value="start" id="btnstart" />
  50. <input type="button" value="stop" id="btnstop" />
  51. <input type="button" value="clear" id="btnclear" />
  52. </body>
  53. <html>
Output
Output
Live Demo

Brief of Setinterval

It is similar to the settimeout function, but it repeats a call to the same function at a given time.

FYI as recap

Settimeout won’t repeat the call, it just calls a function only once.
Syntax of Setinterval
var id = setInterval(fn, delay);
  1. First Parameter(fn) => Any function name
  2. Second Parameter(delay) => specifies the no of milliseconds to wait between each execution.
Note

By using this advantage of setinterval, we can just avoid the recursive call which was implemented in the above settimeout program.
Example
This time, we implemented the same stopwatch functionality also using setinterval to differentiate and understand easily.
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. window.onload = function() {
  5. var startbutton = document.getElementById("btnstart");
  6. var stopbutton = document.getElementById("btnstop");
  7. var clearbutton = document.getElementById("btnclear");
  8. var seconds = 0,
  9. minutes = 0,
  10. hours = 0,
  11. currenttimervalue = 0;
  12. //start of button click logics
  13. //call timer logic when startbutton clicks
  14. startbutton.onclick = timerlogicintiator;
  15. //stop the watch by using clearTimeout function
  16. stopbutton.onclick = function() {
  17. clearTimeout(currenttimervalue);
  18. }
  19. clearbutton.onclick = function() {
  20. document.getElementById("res").innerHTML = "00:00:00";
  21. seconds = 0, minutes = 0, hours = 0;
  22. }
  23. //end of button click logics
  24. function CalculateTimerPartsAndDisplay() {
  25. //increment logic
  26. seconds++;
  27. if (seconds >= 60) {
  28. seconds = 0;
  29. minutes++;
  30. if (minutes >= 60) {
  31. minutes = 0;
  32. hours++;
  33. } //end of if(minutes>= 60)
  34. } //end of if(seconds>= 60)
  35. //display
  36. document.getElementById("res").innerHTML = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds ? (seconds > 9 ? seconds : "0" + seconds) : "00")
  37. } //end of function CalculateTimerPartsAndDisplay()
  38. function timerlogicintiator() {
  39. currenttimervalue = setInterval(CalculateTimerPartsAndDisplay, 1000);
  40. } //end of function timerlogicintiator()
  41. //initial call of timer logic
  42. timerlogicintiator();
  43. } //end of onload function
  44. </script>
  45. </head>
  46. <body>
  47. <h1 id="res">00:00:00</h1> <input type="button" value="start" id="btnstart" /> <input type="button" value="stop" id="btnstop" /> <input type="button" value="clear" id="btnclear" /> </body>
  48. <html>
Output
Output
Demo link to the above program.

Brief of Cleartimeout

It is used to cancel the execution at any time using the timerid, which was returned.
Syntax of ClearTimeout
clearTimeout(id);
Demo link

Conclusion

I hope the above examples were useful. Kindly let me know your feedback or thoughts.