Node.js Timer modules exposes a global API which is used to schedule functions to be called later at some given time. Node.js timer is a construct which is used
to calls a particular function after a given time. Also when the function is called depends upon which timer function is associated with it while creating it and
also what work node.js event loop is doing.
We don't need to explicitly call timers
function because of their global access.
These methods are used to schedule the exection of the code after a previously set period of time.
setImmediate
will be the function to execute. An example is given below :
function hello () {
console.log("This will run Immediately");
}
console.log("It will Print the data Immediately")
setImmediate(hello);
>node "setImmediate().js"
It will Print the data Immediately
This will run Immediately
setInterval()
to execute that code. This method takes a function as its first argument which will run infinitely with a delay given in milliseconds as the second argument.
We can pass more additional arguments also but it is optional and depends upon coders requirements .Also, the delay should be treated as a approximate
delay because it depends on the state and operations
that may be hold on to the event loop. An example is given below :
function hello () {
console.log("This will run recursively");
}
console.log("It will Print the data recursively after a delay of 2000ms again and again")
setInterval(hello,2000);
>node "setInterval().js"
It will Print the data recursively after a delay of 2000ms again and again
This will run recursively
This will run recursively
This will run recursively
This will run recursively
This will run recursively
This will run recursively
This will run recursively
function hello () {
console.log("This will run only once");
}
console.log("It will Print the data once after the delay of 2000ms")
setTimeout(hello,2000);
>node "setTimeout().js"
It will Print the data once after the delay of 2000ms
This will run only once
setImmediate()
function upon its execution.
function hello () {
console.log("This will not run at all");
}
console.log("It is supposed to print the data immediately")
var imm = setImmediate(hello);
clearImmediate(imm);
>node "clearImmediate().js"
It is supposed to print the data immediately
setInterval()
function upon its execution.
function hello () {
console.log("This will Not Run at all");
}
console.log("It is supposed Print the data recursively after a delay of 2000ms again and again")
var s_int = setInterval(hello,2000);
clearInterval(s_int);
>node "clearInterval().js"
It is supposed Print the data recursively after a delay of 2000ms again and again
setTimeout()
function upon its execution.
function hello () {
console.log("This will not run at all");
}
console.log("It is supposed to Print the data once after the delay of 2000ms")
var tim = setTimeout(hello,2000);
clearTimeout(tim);
>node "clearImmediate().js"
It is supposed to Print the data once after the delay of 2000ms
In this chapter of 30 days of node tutorial series, we learned about timers in node.js. We also learned about setting timers and clearing timers.