How to Use JavaScript’s Setinterval()

Posted by

  1. To use setInterval(), you need to include the following line of code in your script:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

const intervalID = setInterval(callback, delay);

[/dm_code_snippet]

The callback parameter is the function or code block that you want to execute at the specified interval. The delay parameter is the interval in milliseconds, so an interval of 1000 milliseconds (1 second) would mean that the function is called every second.

  1. The setInterval() function returns an interval ID that you can use to clear the interval later using clearInterval(). You should store the interval ID in a variable so that you can reference it later.
  2. The callback function can be a function declaration, a function expression, or an arrow function. Here is an example using a function declaration:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

function printMessage() {
  console.log('Hello, world!');
}

const intervalID = setInterval(printMessage, 2000);

[/dm_code_snippet]

And here is an example using an arrow function:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

const intervalID = setInterval(() => {
  console.log('Hello, world!');
}, 2000);

[/dm_code_snippet]

  1. If you want to pass arguments to the callback function, you can use an anonymous function or an arrow function to wrap the call to the callback function:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

const intervalID = setInterval(() => {
  printMessage('Hello, world!');
}, 2000);

[/dm_code_snippet]

  1. To clear the interval, you can use the clearInterval() function and pass it the interval ID:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

clearInterval(intervalID);

[/dm_code_snippet]

clearInterval(intervalID);

Here are some more advanced examples of using setInterval() in JavaScript:

Example 1: Repeating a function every 5 seconds

In this example, we use setInterval() to call a function every 5 seconds (5000 milliseconds):

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

function printMessage() {
  console.log('Hello, world!');
}

const intervalID = setInterval(printMessage, 5000);

[/dm_code_snippet]

Example 2: Repeating a function with arguments

In this example, we use an anonymous function to pass arguments to the callback function:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

function printMessage(message) {
  console.log(message);
}

const intervalID = setInterval(() => {
  printMessage('Hello, world!');
}, 2000);

[/dm_code_snippet]

Example 3: Stopping the interval

In this example, we use clearInterval() to stop the interval after 10 seconds:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

function printMessage() {
  console.log('Hello, world!');
}

const intervalID = setInterval(printMessage, 1000);

setTimeout(() => {
  clearInterval(intervalID);
}, 10000);

[/dm_code_snippet]