Home > Web Front-end > JS Tutorial > body text

Methods to stop execution of setInterval and setTimeout events in JavaScript_javascript tips

WBOY
Release: 2016-05-16 15:59:16
Original
1181 people have browsed it

When executing loop events in js code, the two methods setInterval and setTimeout are often used. The details of these two methods will not be discussed in detail here. Let’s briefly share how to operate when you need to stop the loop event.

(1) The setInterval method can call a function or calculate an expression according to a specified period (in milliseconds). To stop this method, use the clearInterval method. Specific examples are as follows:

Copy code The code is as follows:









Syntax clearInterval(id_of_setinterval)

The parameter id_of_setinterval represents the ID value returned by setInterval().

The clearInterval() method can cancel the timeout set by setInterval(); the parameter of the clearInterval() method must be the ID value returned by setInterval().

(2) The setTimeout method is used to call a function or calculate an expression after a specified number of milliseconds. This method can be stopped using the clearTimeout method. Specific examples are as follows:

Tip: setTimeout() only executes code once. If you want to call it multiple times, use setInterval() or have the code itself call setTimeout() again.

Copy code The code is as follows:













The clearTimeout() method cancels the timeout set by the setTimeout() method.

Syntax clearTimeout(id_of_settimeout)

The parameter id_of_setinterval represents the ID value returned by setTimeout(). This value identifies the deferred execution code block to be canceled.

Related labels:
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