Node.js | Timers module

The Timers module in Node.js contains various functions that allow us to execute a block of code or a function after a set period of time. The Timers module is global, we do not need to use require() to import it.

Timers module has the following functions:

  1. Scheduling Timers: It is used to call a function after a set period of time.

    • setImmediate()
    • setInterval()
    • setTimeout()
  2. Cancelling Timers: It is used to cancel the scheduled timer.

    • clearImmediate()
    • clearInterval()
    • clearTimeout()

1. setImmediate() method: It schedules the “immediate” execution of the callback after I/O events callbacks. In the following example, multiple setImmediate functions are called. Whenever we do these callback functions are queued for execution in the order in which they are created. The entire callback queue is processed after every event loop iteration. If an immediate timer is queued from inside an executing callback, that timer will not be triggered until the next event loop iteration.



filter_none

edit
close

play_arrow

link
brightness_4
code

setImmediate(function A() {
    setImmediate(function B() {
      console.log(1);
      setImmediate(function D() { 
        console.log(2);
      });
    });
  
    setImmediate(function C() {
      console.log(3);
      setImmediate(function E() { 
        console.log(4);
      });
    });
});
  
console.log('Started...');

chevron_right


Output:

Started...
1
3
2
4

In the above example, on the first iteration of event loop function A is fired. Then on second iteration function B is fired, and C is fired on the third iteration. Similarly functions D and E are fired on the fourth and fifth iteration respectively.

2. setInterval() method: It repeats the execution of the callback after every t time in milliseconds passed as a parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Executed after every 1000 milliseconds
// from the start of the program
setInterval(function A() {
    return console.log('Hello World!');
}, 1000);
  
// Executed right away
console.log('Executed before A...');

chevron_right


Output:

Executed before A...
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
...

3. setTimeout() method: It schedules the execution of the callback after a certain time in milliseconds which is passed as a parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Executed after 3000 milliseconds 
// from the start of the program
setTimeout(function A() {
    return console.log('Hello World!');
}, 3000);
  
// executed right away
console.log('Executed before A...');

chevron_right


Output:

Executed before A...
Hello World!

4. clearImmediate() method: It is used to simply cancel the Immediate object created by setImmediate() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

var si = setImmediate(function A() {
    console.log(1);
});
  
// clears setInterval si
clearImmediate(si);
  
console.log(2);

chevron_right


Output:

2

5. clearInterval() method: It is used to cancel the Immediate object created by setInterval() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

var si = setInterval(function A() {
    return console.log("Hello World!");
}, 500);
  
setTimeout(function() {
    clearInterval(si);
}, 2000);

chevron_right


The clearInterval() clears the setInterval ‘si’ after 500 ms, then function A is executed four times.

Output:

Hello World!
Hello World!
Hello World!
Hello World!

6. clearTimeout() method: It is used to cancel the Immediate object created by setTimeout() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// si1 is cleared by clearTimeout()
var si1 = setTimeout(function A() {
    return console.log("Hello World!");
}, 3000);
  
// only si2 is executed
var si2 = setTimeout(function B() {
    return console.log("Hello Geeks!");
}, 3000);
  
clearTimeout(si1);

chevron_right


Only setInterval ‘si2’ is executed as ‘si1’ is cleared by clearTimeout() method.
Output:

Hello Geeks!



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.