Tuesday, 29 May 2018

010 Node.js Timer

Timer functions are globals, there is no need to call require('timers').


setTimeout : Established a function to be called after a mentioned milliseconds. 
Example
setTimeout(function()

    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); 
output :











setImmediate : It is used to execute setImmediate.

Example 
setImmediate(function(){
    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);

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);
 
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);


output :

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...