| by Arround The Web | No comments

How Timers Work in Node.js: A Practical Guide

In Node.js, the “Timers” module schedules the task that can be performed after a specific time interval. It provides a convenient way of scheduling functions that can be called later at a given time. This module performs this main functionality with the help of its several built-in methods. In addition, like other Node.js modules, the user does not need to import this module with the help of the “require()” as all of its methods are available globally.

This write-up explains the detailed guide on the working of the “Timers” module.

How Do Timers Work in Node.js?

The working of the “Timers” module is based on its built methods that execute the code after the specified time interval. These built-in methods are listed below:

Method Description
setTimeout() It performs the defined functionality over the particular elements after the specific time interval.
setImmediate() It executes the specified callback function right after finishing the execution of the current event loop.
setInterval() It runs the code block for infinite times after a specified time interval.

 
Let’s discuss the above-defined methods of the “Timers” module one by one.

Method 1: setTimeout()

The “setTimeout()” performs a certain functionality or operation over the targeted elements after the specific time interval. This is widely used in e-commerce websites to clear the instance of a single user or customer when a certain threshold time period has been reached.

How to Use setTimeout() Function for Timed Execution?

The “setTimeout()” function is solely used to perform a chunk of code after a specific time interval has elapsed. This executes the function or task only once, the programmer can also pass values in the form of parameters after a specific time interval has elapsed.

Syntax

The syntax for the setTimeout() function for timed execution is shown below:

setTimeout( func(), delayTime, args... )

 
In the above-provided syntax:

    • The “func()” specifies a function that needs to be executed after the specified time interval has elapsed.
    • The “delayTime” is the time in milliseconds after this time interval the function provided in the first parameter will be invoked.
    • The “args” are arguments that will be passed to the “func()” function when the time interval elapsed.

Return Value: The “setTimeout()” method returns an interval ID that is used as an argument of the “clearTimeout()” method to clear the given set interval.

Follow the detailed guide on “setTimeout() Method in Node.js” for practical demonstration.

Method 2: setImmediate()

The “Timers” module offers a predefined “setImmediate()” method that executes the specified callback function right after finishing the execution of the current event loop. Its main purpose is to break the long-running operations and to execute the callback function immediately when the other operations(updates, events) execution is completed.

How to Use “setImmediate()” for Immediate Execution?

The “setImmediate()” method is used to execute the code immediately without any delay in the next iteration of the event loop. It works similarly to the “setTimeout()” with zero delay.

Syntax

The use of the “setImmediate()” method relies on its generalized syntax that is stated below:

setImmediate(functionToExecute, [,...args] )

 
According the above syntax, the “setImmediate()” method accepts following parameters:

    • functionToExecute: It represents the function that executes immediately after the execution of all the script’s statements.
    • args(optional): It represents an array of optional parameters that pass to the specified function as its input parameters.

Return Value: The above method returns an ID or a unique timer identifier that can be used in the future with the “clearImmediate()” method.

Follow the detailed guide on “setImmediate() Method in Node.js” for practical demonstration.

Method 3: setInterval()

The  “setInterval()” method executes the code block for infinite times after a specified time interval. This method helps in scheduling the program by performing the common task continuously after a particular delay as per requirements. It performs the defined task with the help of the callback function. Once the desired task is defined in the callback function, then it will automatically execute infinitely without the user’s involvement.

How to Effectively Use setInterval() in Node.js

The “setInterval()” method is used for the execution of a code block repeatedly after the specified time delay. It performs the defined task repeatedly after the specified time interval until the user does not stop its execution using the “clearInterval()” method.

Syntax

The use of the “setInterval()” method depends on its generalized syntax which is written below:

const intervalId = setInterval(func, [delay, arg1, agr2, ..., argN]);

 
The above “setInterval()” method works on the following parameters:

    • func: It denotes a callback function that executes repeatedly for an infinite number of times after the specified time interval.
    • delay: It specifies no of milliseconds after which the defined callback function will execute.
    • arg1, arg2,…argN: It represents the additional arguments that pass to the specified callback function.

Return Value: The “setInterval()” returns a non-zero “intervalId” that the user can pass to the “clearInterval()” method to stop the infinite execution of the callback function.

Follow the detailed guide on “setInterval() Method in Node.js” for practical demonstration.

It’s all about the working of the “Timers” module in Node.js.

Conclusion

In Node.js, the “Timer” module provides a convenient way to perform the desired functionality after a specified interval of time. It performs the defined task with the help of its built-in “setTimeout()”, “setImmediate()”, and the “setInterval()”. Once the operations are performed within the given time interval, then use the “clearTimeout()”, “clearImmediate()”, and the “clearInterval()” methods to clear the set intervals. This post has illustrated the working of the “Timers” module in detail.

Share Button

Source: linuxhint.com

Leave a Reply