30 Days of node
Day 20 : Timers in node.js






30 days of node - Nodejs tutorial series
Timers

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.

Set timer methods

These methods are used to schedule the exection of the code after a previously set period of time.

  1. setImmediate(callback, args*) : This method will execute the code after the end of current cycle of the event loop. This method will be executed after any I/O operations which are in the current event loop but before timers scheduled for the next cycle of event loop.
    First argument to the setImmediate will be the function to execute. An example is given below :
    Note : ** indicates optional arguments passed at run time.
    											
    function hello () {  
      console.log("This will run Immediately");  
    }  
    console.log("It will Print the data Immediately")
    setImmediate(hello);
    											
    										


    We can run it in the following way :
    											
    >node "setImmediate().js"
    It will Print the data Immediately
    This will run Immediately
    											
    										

  2. setInterval(callback, delay , args**) : If there is a block of code that we want to run multiple times , we can use 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 :
    Note : ** indicates optional arguments.
    											
    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);
    											
    										


    We can run it in the following way :
    											
    >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
    											
    										

  3. setTimeout(callback, delay , args** ) : This method can be used to execute a function after a designated amount of delay. This method accepts function as its first argument and the delay in milliseconds as second argument.We can pass more additional arguments also but it is optional and depends upon coders requirements. An example is given below :
    Note : ** indicates optional arguments
    											
    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); 
    											
    										


    We can run it in the following way :
    											
    >node "setTimeout().js"
    It will Print the data once after the delay of 2000ms
    This will run only once
    
    											
    										

Clear timer methods

  1. clearImmediate(immediate) : This method is used to clear the object created by 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);
    											
    										


    We can run it in the following way :
    											
    >node "clearImmediate().js"
    It is supposed to print the data immediately
    											
    										

  2. clearInterval(timeout): This method is used to clear the object created by setInterval() function upon its execution.
    Note : ** indicates optional parameters
    											
    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);
    											
    										


    We can run it in the following way :
    											
    >node "clearInterval().js"
    It is supposed Print the data recursively after a delay of 2000ms again and again
    											
    										

  3. clearTimeout(timeout) : This method is used to clear the object created by 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);
    											
    										


    We can run it in the following way :
    											
    >node "clearImmediate().js"
    It is supposed to Print the data once after the delay of 2000ms
    
    											
    										

Summary

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.