Timer functions are globals, there is no need to call require('timers').
setImmediate : It is used to execute setImmediate.
output :
setTimeout : Established a function to
be called after a mentioned milliseconds.
Example
setTimeout(function()
{
console.log("setTimeout: 2000 millisecond completed!..");
}, 2000);
{
console.log("setTimeout: 2000 millisecond completed!..");
}, 2000);
output :
setInterval : Same function like setTimeout, but it callback repeatedly after every passing of the specified
duration.
Example
setInterval(function()
{
console.log("setInterval: 2000 millisecond completed!..");
}, 2000);
{
console.log("setInterval: 2000 millisecond completed!..");
}, 2000);
output :
setImmediate : It is used to execute setImmediate.
Example
setImmediate(function(){
console.log("SETIMMEDIATE");
});
console.log("SETIMMEDIATE");
});
output :
SETIMMEDIATE
clearTimeout : It prevents object created by setTimeout.
Example
var timerObj = setTimeout(function(str1, str2) {
console.log(str1 + " " + str2);
}, 2000, "Hello.", "world");
clearTimeout(timerObj);
console.log(str1 + " " + str2);
}, 2000, "Hello.", "world");
clearTimeout(timerObj);
output :
clearImmediate: Method can be used to cancel the execution of the callback and created by setImmediate
Example
var myObj = setImmediate(function(str1, str2) {
console.log(str1 + " " + str2);
}, "Hello.", "world");
clearTimeout(myObj);
console.log(str1 + " " + str2);
}, "Hello.", "world");
clearTimeout(myObj);
output :
Hello. world
clearInterval : It is used to stop an intervalObject, as created by setInterval.
Example
var myTimerInterval = setInterval(function(str1, str2) {
console.log(str1 + " " + str2);
}, 1000, "Hello.", "World");
clearInterval(myTimerInterval);
console.log(str1 + " " + str2);
}, 1000, "Hello.", "World");
clearInterval(myTimerInterval);
output :