setInterval() method

Another built-in JavaScript function that allows you to run a function or evaluate an expression repeatedly at certain time interval. It also takes same two parameters.

Syntax:

setInterval(function, interval);
  • function: This parameter specifies the function to be executed or the code to be evaluated at each interval.
  • interval: This parameter specifies the time interval in milliseconds between each execution of the function.

Example: The below code implements the setInterval method with a basic example.

JavaScript
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body style="text-align: center;">
    <h2>
        JavaScript setInterval
    </h2>
    <h3>
        The below count value will increase
        <br /> automatically after evry one second.
    </h3>
    <h4 id="result">
        Count: 0
    </h4>
    <script>
        let count = 0;
        const result = document.getElementById('result');
        let intervalId = setInterval(() => {
            count++;
            result.innerHTML = `Count: ${count}`;
        }, 1000);
    </script>
</body>

</html>

Output:

Difference Between setTimeout & setInterval

JavaScript provides two essential functions: setTimeout and setInterval. While both serve similar purposes, they have distinct differences that developers should be aware of to effectively manage timing-related tasks in their code.

Similar Reads

setTimeout() method

A built-in JavaScript function called setTimeout allows you to run a function or evaluate an expression after a predetermined millisecond delay. It requires two parameters: the code or function to be evaluated and the delay before execution....

setInterval() method

Another built-in JavaScript function that allows you to run a function or evaluate an expression repeatedly at certain time interval. It also takes same two parameters....

Difference between the setTimeout and setInterval method

AspectsetTimeoutsetIntervalExecution Runs a function once after a specified delay. Runs a function repeatedly at specified intervals. Parameters Takes a function and a delay (in milliseconds). Takes a function, a delay, and an optional repetition count. Interval Control No automatic repetition, manual scheduling needed. Automatically repeats based on the specified interval. Single Execution Executes the function only once. Can execute the function multiple times until cleared. Clearing Use clearTimeout() method to stop execution. Use clearInterval() method to stop repetition. Resource Usage Consumes fewer resources as it runs only once. Consumes more resources as it runs repeatedly. Precision Offers better precision for single-time execution. May suffer from timing drift over long periods. Control Provides control over when the function executes. May lack precise control over execution timing. Use Cases Suitable for one-time events, delays, or timeouts. Ideal for tasks needing continuous repetition, like animations....