Node.js Timer

Node.js Timer

In Node.js, timer functions are global functions, which means you don’t need to import or require any module to use them. These functions are commonly used to schedule code execution after a delay, repeatedly at intervals, or immediately after the current event loop phase.

Introduction to Applied AI:–Click Here

Let’s go through the main timer functions available in Node.js.

Set Timer Functions

  • setImmediate() → Executes a callback immediately after the current event loop phase.
  • setInterval() → Executes a callback repeatedly after a specified interval.
  • setTimeout() → Executes a one-time callback after a specified delay in milliseconds.

Data Science Tutorial:-Click Here

Clear Timer Functions

  • clearImmediate(immediateObject) → Stops an immediateObject created by setImmediate().
  • clearInterval(intervalObject) → Stops an intervalObject created by setInterval().
  • clearTimeout(timeoutObject) → Cancels a timeoutObject created by setTimeout().

Download New Real Time Projects :-Click here

Node.js setInterval() Example

This example prints a message every 1000 milliseconds until the process is terminated.

File: timer1.js

setInterval(function() {  
  console.log("setInterval: Hey! 1 second completed!..");   
}, 1000);  

Run the file in the Node.js command prompt:

node timer1.js

Example 2 – Incrementing Counter

Machine Learning Tutorial:–Click Here

File: timer5.js

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

Run with:

node timer5.js

Node.js setTimeout() Example

Complete Advance AI topics:- CLICK HERE

Example 1 – Basic Timeout

File: timer1.js

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

Run with:

node timer1.js

Example 2 – Recursive Timeout

File: timer2.js

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

Run with:

node timer2.js

This demonstrates how setTimeout() can be used recursively to mimic the behavior of setInterval().

Deep Learning Tutorial:– Click Here

Using clearTimeout() and clearInterval()

Example 1 – Clearing Timeout

File: timer3.js

function welcome () {  
  console.log("Welcome to Updategadh!");  
}  

var id1 = setTimeout(welcome, 1000);  
var id2 = setInterval(welcome, 1000);  

clearTimeout(id1);  
//clearInterval(id2);  

Download New Real Time Projects :–Click here

Run with:

node timer3.js

In this case, the timeout is cleared, so only the interval will run.

Complete Python Course with Advance topics:-Click Here

Example 2 – Clearing Interval

File: timer3.js

function welcome () {  
  console.log("Welcome to Updategadh!");  
}  

var id1 = setTimeout(welcome, 1000);  
var id2 = setInterval(welcome, 1000);  

//clearTimeout(id1);  
clearInterval(id2);  

SQL Tutorial :–Click Here

Run with:

node timer3.js

Here, the interval is cleared, so only the single timeout executes.


node js setinterval
nodejs settimeout
nodejs timer example
node:timers/promises
nodejs.timer typescript
node:timers/promises settimeout
node js sleep
nodejs setimmediate
node js timer example
node js timer download
node js timer w3schools
node js timer tutorial

    Share this content:

    Post Comment