HI WELCOME TO SIRIS

Node.js Timer

Leave a Comment
Node.js Timer functions are global functions. You don't need to use require() function in order to use timer functions. Let's see the list of timer functions.
Set timer functions:
  • setImmediate(): It is used to execute setImmediate.
  • setInterval(): It is used to define a time interval.
  • setTimeout(): ()- It is used to execute a one-time callback after delay milliseconds.
Clear timer functions:
  • clearImmediate(immediateObject): It is used to stop an immediateObject, as created by setImmediate
  • clearInterval(intervalObject): It is used to stop an intervalObject, as created by setInterval
  • clearTimeout(timeoutObject): It prevents a timeoutObject, as created by setTimeout

Node.js Timer setInterval() Example

This example will set a time interval of 1000 millisecond and the specified comment will be displayed after every 1000 millisecond until you terminate.
File: timer1.js
  1. setInterval(function() {  
  2.  console.log("setInterval: Hey! 1 millisecond completed!..");   
  3. }, 1000);  
Open Node.js command prompt and run the following code:
  1. node timer1.js  
Node.js timer example 1
File: timer5.js
  1. var i =0;  
  2. console.log(i);  
  3. setInterval(function(){  
  4. i++;  
  5. console.log(i);  
  6. }, 1000);   
Open Node.js command prompt and run the following code:
  1. node timer5.js  
Node.js timer example 22

Node.js Timer setTimeout() Example

File: timer1.js
  1. setTimeout(function() {   
  2. console.log("setTimeout: Hey! 1000 millisecond completed!..");  
  3. }, 1000);  
Open Node.js command prompt and run the following code:
  1. node timer1.js  
Node.js timer example 21
This example shows time out after every 1000 millisecond without setting a time interval. This example uses the recursion property of a function.
File: timer2.js
  1. var recursive = function () {  
  2.     console.log("Hey! 1000 millisecond completed!..");   
  3.     setTimeout(recursive,1000);  
  4. }  
  5. recursive();   
Open Node.js command prompt and run the following code:
  1. node timer2.js  
Node.js timer example 23

Node.js setInterval(), setTimeout() and clearTimeout()

Let's see an example to use clearTimeout() function.
File: timer3.js
  1. function welcome () {  
  2.   console.log("Welcome to JavaTpoint!");  
  3. }  
  4. var id1 = setTimeout(welcome,1000);  
  5. var id2 = setInterval(welcome,1000);  
  6. clearTimeout(id1);  
  7. //clearInterval(id2);  
ADVERTISEMENT
Open Node.js command prompt and run the following code:
  1. node timer3.js  
Node.js timer example 3
You can see that the above example is recursive in nature. It will terminate after one step if you use ClearInterval.

Node.js setInterval(), setTimeout() and clearInterval()

Let's see an example to use clearInterval() function.
File: timer3.js
  1. function welcome () {  
  2.   console.log("Welcome to JavaTpoint!");  
  3. }  
  4. var id1 = setTimeout(welcome,1000);  
  5. var id2 = setInterval(welcome,1000);  
  6. //clearTimeout(id1);  
  7. clearInterval(id2);  
Open Node.js command prompt and run the following code:
  1. node timer3.js  
Node.js timer example 33

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.