Timing and Asynchronous Techniques

Learn about timing functions, asynchronous programming, intervals, and how to implement a stopwatch.

Timing functions

JavaScript provides a couple of useful methods that enable us to schedule when a function is called or to call a function at regular intervals. These can be useful for causing a delay before something happens, or displaying a timer on the screen.

The setTimeout method

The setTimeout() method accepts a callback function as its first parameter and a number of milliseconds as its second parameter. The callback function that’s provided as the first argument will be called after the time given as the second argument.

To see this in action, try running the following code. It should show an alert box after seven seconds. This is because the first argument is an anonymous arrow function that displays the alert box, and the second argument is 70007000 milliseconds, or 77 seconds:

Get hands-on with 1200+ tech skills courses.