Home > Web Front-end > JS Tutorial > How Do I Stop a setInterval in JavaScript?

How Do I Stop a setInterval in JavaScript?

DDD
Release: 2024-12-16 20:12:17
Original
978 people have browsed it

How Do I Stop a setInterval in JavaScript?

Stopping setInterval Functionality in JavaScript

In JavaScript, the setInterval() method allows you to execute a function at a specified interval. However, there may be scenarios where you need to stop the repeated invocation of this function on a particular event.

To achieve this, you can utilize the interval ID returned by setInterval(). This ID serves as a unique identifier for the interval, and you can use it with the clearInterval() method to halt the function execution.

Here's an example:

var refreshIntervalId = setInterval(fname, 10000); // Set the interval (10 seconds)

/* Later, when you want to stop the interval */
clearInterval(refreshIntervalId);
Copy after login

By invoking clearInterval(refreshIntervalId), you effectively stop the repeated execution of fname, allowing you to control the refresh process according to the desired user interaction or event.

The above is the detailed content of How Do I Stop a setInterval in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template