Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to use Timers in Node.js, as Node allows the users to reuse the timers' existing learning.

In Node.js, the Timers module comprises several functions that allow the users to process a snippet of code or a function after a fixed period. The Timers module is a core module of Node.js. However, we don't need to import the module using the require() function.

Functions of Timers module

The Timers module has two main functions:

  1. Scheduling the Timers: This Function allows the users to call a function after a fixed period. This function includes the following methods:
  2. setTimeout()
  3. setInterval()
  4. setImmediate()
  5. Canceling the Timers: This Function allows the users to cancel or withdraw the scheduled timer. This function includes the following methods:
  6. clearTimeout()
  7. clearInterval()
  8. clearImmediate()

Let's discuss these methods in brief.

 setTimeout()

The setTimeout() method is used to program the callback's execution after a definite time in milliseconds, delivered as a parameter.

// This function will execute
// after 5000 milliseconds  
setTimeout(function A() { 
    return console.log('Welcome to TUTORIALANDEXAMPLE.COM'); 
}, 5000); 
// This function will execute right away
console.log('Hello World!'); 

The Output of the above snippet of code should look as follows:

Hello World!
Welcome to TUTORIALANDEXAMPLE.COM

setInterval()

The setInterval() method is used to repeat the callback execution after every time t, in milliseconds, delivered as a parameter.

// This function will execute
// after every 500 milliseconds  
setInterval(function A() { 
    return console.log('Welcome to TUTORIALANDEXAMPLE.COM'); 
}, 500); 
// This function will execute right away
console.log('Hello World!'); 

The Output of the above snippet of code should look as follows:

Hello World!
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
...

setImmediate()

The setImmediate() method is used to program the “immediate” callback execution after the callback of I/O events.

setImmediate(function A() { 
    setImmediate(function B() { 
      console.log(3); 
      setImmediate(function D() {  
        console.log(2); 
      }); 
    }); 
    setImmediate(function C() { 
      console.log(1); 
      setImmediate(function E() {  
        console.log('Hello World!'); 
      }); 
    }); 
}); 
console.log('The Function is starting in...');

The Output of the above snippet of code should look as follows:

The Function is starting in...
3
1
2
Hello World!

clearTimeout()

The clearTimeout() method is used to cancel or clear the Immediate instance build by the setTimeout() method.

// clearTimeout() cancels the first function
var first = setTimeout(function A() { 
    return console.log("Welcome to TUTORIALANDEXAMPLE.COM"); 
}, 2000); 
// only second function is executed
var second = setTimeout(function B() { 
    return console.log("Hello World!"); 
}, 2000); 
clearTimeout(first); 

The Output of above snippet of code should look as follows:

Hello World!

clearInterval()

The clearInterval() method is used to cancel or clear the Immediate instance build by the setInterval() method.

var first = setInterval(function A() { 
    return console.log("Welcome to TUTORIALANDEXAMPLE.COM"); 
}, 500); 
setTimeout(function() { 
    clearInterval(first); 
}, 2500); 

The Output of above snippet of code should look as follows:

Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM

clearImmediate()

The clearImmediate() method is used to cancel or clear the Immediate instance build by the setImmediate() method.

var first = setImmediate(function A() { 
    console.log('Hello World!'); 
}); 
clearImmediate(first); 
console.log('Welcome to TUTORIALANDEXAMPLE.COM'); 

The Output of above snippet of code should look as follows:

Welcome to TUTORIALANDEXAMPLE.COM