From the course: Node.js Essential Training

Unlock the full course today

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

Creating a delay with setTimeout

Creating a delay with setTimeout - Node.js Tutorial

From the course: Node.js Essential Training

Creating a delay with setTimeout

- [Instructor] In the last lesson, we used Node.js asynchronously by using event listeners. So what we said here was instead of just running the functions when they are added to our JavaScript file, we said only call the function that's sent to this function when we have a data event, and only send this function when we have an exit event. So call these functions at specific times. Now, another way that we can work with Node.js asynchronously is through timing functions. So the timing functions like set timeout, clear timeout, set interval, and clear interval work the same way that they do in the browser, and are available to us globally. So let's go ahead and create a set timeout function. We're going to create a new variable here called waitTime. So we'll say const waitTime. And we'll set that to 3,000, which means three seconds or 3,000 milliseconds. Then we're going to call a console log, and we'll say setting a…

Contents