×

Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to use Timers in Node.js, as Node allows the users to reuse the timers' existing learning.

In Node.js, the Timers module comprises several functions that allow the users to process a snippet of code or a function after a fixed period. The Timers module is a core module of Node.js. However, we don't need to import the module using the require() function.

Functions of Timers module

The Timers module has two main functions:

  1. Scheduling the Timers: This Function allows the users to call a function after a fixed period. This function includes the following methods:
  2. setTimeout()
  3. setInterval()
  4. setImmediate()
  5. Canceling the Timers: This Function allows the users to cancel or withdraw the scheduled timer. This function includes the following methods:
  6. clearTimeout()
  7. clearInterval()
  8. clearImmediate()

Let's discuss these methods in brief.

 setTimeout()

The setTimeout() method is used to program the callback's execution after a definite time in milliseconds, delivered as a parameter.

// This function will execute
// after 5000 milliseconds  
setTimeout(function A() { 
    return console.log('Welcome to TUTORIALANDEXAMPLE.COM'); 
}, 5000); 
// This function will execute right away
console.log('Hello World!'); 

The Output of the above snippet of code should look as follows:

Hello World!
Welcome to TUTORIALANDEXAMPLE.COM

setInterval()

The setInterval() method is used to repeat the callback execution after every time t, in milliseconds, delivered as a parameter.

// This function will execute
// after every 500 milliseconds  
setInterval(function A() { 
    return console.log('Welcome to TUTORIALANDEXAMPLE.COM'); 
}, 500); 
// This function will execute right away
console.log('Hello World!'); 

The Output of the above snippet of code should look as follows:

Hello World!
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
...

setImmediate()

The setImmediate() method is used to program the “immediate” callback execution after the callback of I/O events.

setImmediate(function A() { 
    setImmediate(function B() { 
      console.log(3); 
      setImmediate(function D() {  
        console.log(2); 
      }); 
    }); 
    setImmediate(function C() { 
      console.log(1); 
      setImmediate(function E() {  
        console.log('Hello World!'); 
      }); 
    }); 
}); 
console.log('The Function is starting in...');

The Output of the above snippet of code should look as follows:

The Function is starting in...
3
1
2
Hello World!

clearTimeout()

The clearTimeout() method is used to cancel or clear the Immediate instance build by the setTimeout() method.

// clearTimeout() cancels the first function
var first = setTimeout(function A() { 
    return console.log("Welcome to TUTORIALANDEXAMPLE.COM"); 
}, 2000); 
// only second function is executed
var second = setTimeout(function B() { 
    return console.log("Hello World!"); 
}, 2000); 
clearTimeout(first); 

The Output of above snippet of code should look as follows:

Hello World!

clearInterval()

The clearInterval() method is used to cancel or clear the Immediate instance build by the setInterval() method.

var first = setInterval(function A() { 
    return console.log("Welcome to TUTORIALANDEXAMPLE.COM"); 
}, 500); 
setTimeout(function() { 
    clearInterval(first); 
}, 2500); 

The Output of above snippet of code should look as follows:

Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM
Welcome to TUTORIALANDEXAMPLE.COM

clearImmediate()

The clearImmediate() method is used to cancel or clear the Immediate instance build by the setImmediate() method.

var first = setImmediate(function A() { 
    console.log('Hello World!'); 
}); 
clearImmediate(first); 
console.log('Welcome to TUTORIALANDEXAMPLE.COM'); 

The Output of above snippet of code should look as follows:

Welcome to TUTORIALANDEXAMPLE.COM

Related Topics

Difference Between Node.js and PHP

Node.js vs PHP Node.js and PHP both are the powerful back-ends to create versatile websites. Even when these two technologies fall under the same category, their features are pretty diverse. There's...

6 minutes read.

Node.js Events Module

Introduction to the Node.js events Module As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture,...

4 minutes read.

Node.js Error Handling

Node.js – Error Handling: Errors are part of every programming language, and Node.js Environment is not an exception. There are many predefined errors in Node.js, including errors like SyntaxError, RangeError, and...

4 minutes read.

Node.js File System

Introduction to File System in Node.js FS, abbreviated as File System, is a Node.js Build-in Module, which provides an Application Programming Interface (API) to interact with the File System, and to...

15 minutes read.

Node.js Path Module

Understanding the path Module in Node.js The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s...

6 minutes read.

Node.js Event Loop Phases

Node.js – Phases of Event Loop Once we have understood the Node.js event loop concept and the basic introduction to its phases, let's discuss the functions of the Check handler phase...

4 minutes read.

Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to...

4 minutes read.

Node.js Event Loop

An Introduction to Event Loop As we have already discussed, Node.js is an Asynchronous platform with a non-blocking I/O. Node.js has awesome features that made it this thriving, and the Event...

5 minutes read.

Differentiation of Web Scraping and Web Crawling

Most of us get confused between the word’s web scraping and web crawling, and even some people think they are synonyms for each other. But there is a considerable difference between...

3 minutes read.

Node.js Buffers

An Introduction to Buffers A Buffer is an area in memory (typically RAM) that is used to store binary data. Binary is a collection or a set of 0 and 1....

4 minutes read.

Node.js Streams

An Introduction to the Streams Streams are one of the elemental concepts that are introduced to power the applications based on Node.js. These are the method used to handle the data...

6 minutes read.

Node.js Core Debugger

Node.js – Core Debugger: It’s always a challenge when it comes to debug an application. The Asynchronous workflows of Node.js include an additional layer of intricacy to this difficult process....

3 minutes read.

Node.js Modules

Node.js Modules: At the point when we'll begin composing Node.js applications, we would place all our code into one big node.js file, regardless of how enormous or complex our application...

4 minutes read.

Node.js Tutorial

Introduction to Node.js: Node.js is a non-proprietary and platform-independent runtime environment for implementing JavaScript code outside of a browser. However, NodeJS isn't a framework and not a programming language. We...

4 minutes read.

How to Install Node.js on Windows?

Environment Setup for Node.js Since Node.js is a mandatory section of the program that runs JavaScript, similar to all other required section of programs that run a programming language, it is...

4 minutes read.

Node.js OS Module

Introduction to Node.js OS Module The OS Module in Node.js provides the Operating System-related Utilities. This module delivers the functions to interact with the Operating System. It also delivers the Operating...

5 minutes read.

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages. Usually, in JavaScript, a console mechanism is...

4 minutes read.

Node.js HTTP Module

An Introduction to HTTP Module The http module is a core module in Node.js that uses the Hypertext Transfer Protocol (HTTP) to transfer data. With the help of the http module,...

7 minutes read.

Node.js Callback Concept

Understanding the Concept of Callback A Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute...

2 minutes read.

Node.js NPM

Node.js – Node Package Manager NPM is an abbreviation for Node Package Manager. It is the default package manager developed by Isaac Z. Schlueter using JavaScript Programming Language, designed for JavaScript Runtime...

7 minutes read.