NodeJS Timer is used to set the time for execution of an expression, it gives the power to control the time of execution for different expressions.
Node.js is used to create large servers, and process several requests and responses simultaneously, to manage the flow of execution and operation with the time Node.js provide Timers.
You can select which expression executes first, and what the total time taken by the expression to execute. You can set the time limit for the execution of the expression so that if the expression is not able to execute in that time interval it will terminate.
Not only this, there are several functionalities provided by Node.js Timer, which we will understand in this article. But before going further let’s understand the uses of timers by assuming an example.
Suppose we have to create a functionality of sending the confirmation code to the users when they create an account, and after sending the code, wait for 10s then give the option to send it again. So, in this case, we can use the Node.js timer to set a timer that activates automatically when sending the code, the timer hides the option of sending the code again for a particular time, and only after that, it will show the option to send an email.
Node.js Timer Methods
Node.js timer methods are global methods that can be used directly without importing them. Learn more about Node.js globals – https://codeforgeek.com/nodejs-globals/
setImmediate()
This timer method is used to define a function that runs immediately write after the execution. As the name suggests, the function pass as an argument inside this method will be set to immediate execution.
Syntax:
setImmediate(function)
Where function can be a function definition without a name, an arrow function, or a function call.
Example:
const fun = setImmediate(function() { console.log("Immediate"); });
Here we define a function that prints a string immediately when the application runs.
Output:
clearImmediate
This timer method is used to terminate the execution of the function defined by the setImmediate method.
Syntax:
clearImmediate(immediateObject)
Example:
const fun = setImmediate(function() { console.log("Immediate"); });
clearImmediate(fun);
clearImmediate method will stop the execution of setImmediate so we get nothing as an output.
Output:
setInterval
This timer method is used to define a function that continuously runs at a definite interval until its termination.
Syntax:
setInterval(function, time)
Example:
setInterval(function() {
console.log("Running in interval of 1 millisecond!");
}, 1000);
Here we define a function that prints a string at an interval of 1 millisecond.
Output:
clearInterval
This timer method is used to stop the execution of the function defined by the setInterval method.
Syntax:
clearInterval(intervalObject)
Example:
var x = 0;
const fun = setInterval(function() {
x = x + 1;
console.log(x);
if(x == 5) {
clearInterval(fun);
};
}, 1000);
Here we use the setInterval method to change the value of a variable x and print it and if the value becomes 5 then the clearInterval method stops its execution.
Output:
setTimeout
This timer method is used to define a function that runs after a definite time.
Syntax:
setTimeout(function, time)
Example:
setTimeout(function() {
console.log("1 millisecond has passed!");
}, 1000);
Here we define a function that prints a string after 1 millisecond.
Output:
clearTimeout
This timer is used to stop the execution of the function defined by the setTimeout method.
Syntax:
clearTimeout(timeoutObject)
Example:
var x = true;
const fun = setTimeout(function() {
console.log("1 millisecond has passed!");
}, 1000);
if(x){
console.log("X is true!");
clearTimeout(fun);
}
Here we use the setTimeout method to define a function that prints a string after 1 millisecond, but if the value of a variable we define is true it will stop the function so that it will not able to print the value after 1 millisecond.
Output:
Summary
Node.js Timer is used to set the time of execution of an expression, it can also be used to loop through an expression on a time interval until a particular condition becomes false. Hope this article helps you to understand the Node.js Timer.
Reference
https://nodejs.org/en/docs/guides/timers-in-node/