Working with Timers – Node.js

Timers are polyglot. They are very useful in many use cases. Almost all major programming languages supports them and Node.js is no exception. Its actually more simple in Node because Node.js let us reuse our existing learning of timers. In this post, I will describe and demonstrate what they are, how to use them, whats the syntax looks like and then you can leverage those in your applications. One common example, if you want to retrieve data from a REST API on specific interval, you can achieve this 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 dealy 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.

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

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

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

Here is the output of the function call.

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 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).

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).

Canceling Timers

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

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.

Summary

We learned that its 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

  • https://hexquote.com/node-js-introduction/