node js - node js timer - node - node js tutorial - webnode




What is Timer in Node.js?

  • 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
 nodejs timer image

Learn Node js - node js Tutorial - Nodejs Timer Image - node - Node js Examples

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.

Open Node.js command prompt and run the following code:

Node timer1.js  
Node js tutorial tag : node - webnode - nodejs - node js tutorial

Node JS Output :

 Nodejs Timer Example1

Learn Node js - node js Tutorial - Node.js Timer Example1 - node - Node js Examples

Node JS Tutorial File: timer5.js

var i =0;  
console.log(i);  
setInterval(function(){  
++;  
console.log(i);  
}, 1000);   

Open Node.js command prompt and run the following code:

node timer5.js  
Node js tutorial tag : node - webnode - nodejs - node js tutorial

Node JS Output :

 Nodejs Timer Example2

Learn Node js - node js Tutorial - Node.js Timer Example2 - node - Node js Examples

Node.js Timer setTimeout() Example

Node JS Tutorial File: timer1.js

setTimeout(function() {   
console.log("setTimeout: Hey! 1000 millisecond completed!..");  
}, 1000);  

Open Node.js command prompt and run the following code:

node timer1.js  
Node js tutorial tag : node - webnode - nodejs - node js tutorial

Node JS Output :

 Nodejs Timer Example3

Learn Node js - node js Tutorial - Node.js Timer Example3 - node - Node js Examples

  • This example shows time out after every 1000 millisecond without setting a time interval. This example uses the recursion property of a function.

Node JS Tutorial File: timer2.js

var recursive = function () {  
    console.log("Hey! 1000 millisecond completed!..");   
    setTimeout(recursive,1000);  
}  
recursive();   

Open Node.js command prompt and run the following code:

node timer2.js  
Node js tutorial tag : node - webnode - nodejs - node js tutorial

Node JS Output :

 Nodejs Timer Example4

Learn Node js - node js Tutorial - Node.js Timer Example5 - node - Node js Examples

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

  • Let's see an example to use clearTimeout() function.

Node JS Tutorial File: timer3.js

function welcome () {  
  console.log("Welcome to wikitechy!");  
}  
var id1 = setTimeout(welcome,1000);  
var id2 = setInterval(welcome,1000);  
clearTimeout(id1);  
//clearInterval(id2);  

Open Node.js command prompt and run the following code:

node timer3.js  
Node js tutorial tag : node - webnode - nodejs - node js tutorial

Node JS Output :

 Nodejs Timer Example4

Learn Node js - node js Tutorial - Node.js Timer Example5 - node - Node js Examples

  • 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.

Node JS Tutorial File: timer3.js

function welcome () {  
  console.log("Welcome to wikitechy!");  
}  
var id1 = setTimeout(welcome,1000);  
var id2 = setInterval(welcome,1000);  
//clearTimeout(id1);  
clearInterval(id2);  

Open Node.js command prompt and run the following code:

node timer3.js  
Node js tutorial tag : node - webnode - nodejs - node js tutorial

Node JS Output :

 Nodejs Timer Example6

Nodejs Timer Example6


This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js Timer

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×