Timers Module in Node.js
×


Timers Module in Node.js

112

In Node.js, the Timers module enables developers to schedule code execution after a set amount of time or repeatedly at specific intervals. These functions are part of the global object and don’t require importing a separate module. They play an important role in managing asynchronous operations and controlling the timing of function execution in your applications.

Overview of Timers in Node.js

The Timers module offers a set of built-in functions that help execute code after a delay or on a repeating schedule. These functions are non-blocking and rely on the Node.js event loop. Common timer functions include:

  • setTimeout()
  • setInterval()
  • clearTimeout()
  • clearInterval()
  • setImmediate()
  • clearImmediate()

setTimeout()

setTimeout() is used to execute a function after a specified number of milliseconds.

setTimeout(() => {
    console.log("Executed after 2 seconds");
}, 2000);

This function schedules the callback to run once after the given delay.

clearTimeout()

clearTimeout() cancels a timeout that was previously set with setTimeout().

const timer = setTimeout(() => {
    console.log("This will not run");
}, 3000);

clearTimeout(timer);

This prevents the callback from being executed.

setInterval()

setInterval() runs a function repeatedly with a fixed delay between each call.

const intervalId = setInterval(() => {
    console.log("Repeats every 1 second");
}, 1000);

clearInterval()

clearInterval() stops the repeated execution initiated by setInterval().

clearInterval(intervalId);

setImmediate()

setImmediate() is used to execute a function right after the current poll phase of the event loop is complete.

setImmediate(() => {
    console.log("Executed immediately after I/O events");
});

clearImmediate()

clearImmediate() cancels the execution of a function set with setImmediate().

const immediateId = setImmediate(() => {
    console.log("This won't run");
});

clearImmediate(immediateId);

Use Cases of Timers

  • Delaying actions such as API retries or animations
  • Creating periodic tasks like status checks
  • Managing asynchronous behavior in a non-blocking way

Best Practices

  • Always store timer IDs if you plan to clear them later.
  • Avoid deeply nested timers, as they can lead to callback hell.
  • Use setImmediate() when you want your function to run after the current operation completes.

Conclusion

The Timers module in Node.js offers essential tools for handling asynchronous timing in your applications. Whether you're delaying execution with setTimeout() or running repeated actions with setInterval(), these functions are fundamental for building responsive and non-blocking systems.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat