{{announcement.body}}
{{announcement.title}}

Working With Timers in Node.js

DZone 's Guide to

Working With Timers in Node.js

In this post, we will see what timers are in general and how we can use timers in our Node.js applications.

· Web Dev Zone ·
Free Resource

Timers are polyglot. They are very useful in many use cases. Almost all major programming languages supports them, and Node.js is no exception. It's actually more simple in Node because Node lets us reuse our existing learning of timers. 

In this post, I will describe and demonstrate what timers are, how to use them, what the syntax looks like, and then how you can leverage them in your applications. One common example, if you want to retrieve data from a REST API on specific interval, you can achieve very easily using timers. So, even if you are new to JavaScript or timers, this post will help you understand these concepts. If you are interested in knowing more about Node, you can also check my other articles as well. 

Timers

Timers are very popular global functions in browsers environment:

  • setTimeout.
  • setInterval.

Node.js has an API for these functions as well, and it exactly matches the browser’s API.
These functions can be used to delay or repeat the execution of other functions, which they receive as argument.

Timers Example(s) – setTimeout

This code uses setTimeout to delay execution of a function by 4 seconds. Note that function passed as an argument is a function references. It might not be an inline function.

setTimeout example code block

Here is the variation of the same code, where the function is not passed in-line. 

setTimeout example code block

The next screenshot shows another variation, which shows how to pass arguments to function reference. 

setTimeout example code block

So, all the above mentioned examples are very simple and shows you various styles of setting up a setTimeout function.

Timers Exercise

Here is an exercise for you to try. If you like, you can share your solution in the comments section. Also if interested, let me know and I will share my solution as well. So, here is the description for the exercise.

  • Print the following (after their corresponding delays):
    • “Hello after 4 seconds”
    • “Hello after 8 seconds”
  • Constraints:
    • Define only a single function in your script which includes inline function(s). Hint: It might look something like below (again use your own way to achieve this and as you like).

Sample timeout exercise

Timers Example(s) - setInterval

We can use setInterval if we want to repeat some action on time. The following example will printing to console after 3 seconds interval, forever (unless we stop it).

setInterval example code block

Canceling Timers

Another cool thing about timers is that you can cancel them with code. e.g. clearTimeout(timerId). 

clearTimeout example

Instead of clearTimeout you can use SetImmediate for same result. but this is less common.

If you are using setInterval, you can use clearInterval function to cancel that as well. The following example demonstrate that.

setInterval example

Summary

We learned that it's easy to work with timers in Node.js. The code is similar what we were using in JavaScript for browsers. They are very handy functions and can be used in various scenarios. Have you done the exercise yet, give it a try and share your solution with me. Feel free to let me know if you have some questions. Till next time, Happy coding.

Other Links

Topics:
javascript, node.js, setInterval, setTimeout, timers

Published at DZone with permission of Jawad Hasan Shani . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}