From the course: Node.js Essential Training

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Incorporating setInterval

Incorporating setInterval - Node.js Tutorial

From the course: Node.js Essential Training

Incorporating setInterval

- [Instructor] So let's include another type of timer in this same file. The way I'll do that is by creating a new value called interval. Here we're going to define a certain interval where we want to call a function. So it's going to be every half a second, and we'll also set a current time of zero. From here, we're going to create another function called increment time, and increment time is going to be responsible, every 500 milliseconds, for adding a new 500 milliseconds to the current time. So this will continue to accumulate half-a-seconds, kind of like a really fast clock, a clock that ticks every half a second. So we'll say, waitInterval, then we will console-log, "waiting," currentTime, divided by 1,000 seconds. Excellent, so from here, instead of using setTimeout, which is going to time out and call a function after a certain time, we're going to say setInterval. We'll pass in increment time and our…

Contents