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.

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

setInterval()

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

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

setImmediate()

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

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

clearTimeout()

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

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

clearInterval()

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

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

clearImmediate()

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

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

Pin It on Pinterest

Share This