Nearby lessons

15 of 22

JavaScript: setInterval() Method

๐Ÿ“Œ What is setInterval()?

The setInterval() method repeatedly calls a function with a fixed time delay between each call, until it is cleared using clearInterval().

1๏ธโƒฃ Basic Usage of setInterval()

This example shows how to print a message every 2 seconds.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

2๏ธโƒฃ Using Named Function with setInterval()

You can also pass a named function instead of an anonymous function.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

3๏ธโƒฃ Passing Arguments to the Function

You can pass additional arguments to the function being executed by setInterval().

Code Example
PREVIEW READY
Loading Editor...
Live Preview

4๏ธโƒฃ Stopping setInterval() using clearInterval()

You can stop the interval by storing the ID and passing it to clearInterval().

Code Example
PREVIEW READY
Loading Editor...
Live Preview

๐Ÿง  Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!AJS - Callback Functions