Home > Web Front-end > JS Tutorial > SetInterval and setTimeout stop methods_javascript skills

SetInterval and setTimeout stop methods_javascript skills

WBOY
Release: 2016-05-16 18:11:22
Original
1501 people have browsed it

Let’s first understand setInterval:
1, HTML DOM setInterval() method
Definition and usage
The setInterval() method can call functions or calculations according to the specified period (in milliseconds) expression.
The setInterval() method will keep calling the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.
Syntax
setInterval(code,millisec[,"lang"])
Parameter Description
code Required. A function to be called or a string of code to be executed.
millisec is a must. The time interval, in milliseconds, between periodic executions or calls to code.
Return Value
A value that can be passed to Window.clearInterval() to cancel periodic execution of code.
2, HTML DOM clearInterval() method
Definition and usage
clearInterval() method can cancel the timeout set by setInterval().
The parameter of clearInterval() method must be the ID value returned by setInterval().
Syntax
clearInterval(id_of_setinterval)
Parameter Description
id_of_setinterval The ID value returned by setInterval().
How to stop:
The following example will call the clock() function every 50 milliseconds. You can also stop this clock using a button:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]
Let’s learn more about setTimeout :

1,HTML DOM setTimeout() method
Definition and usage
The setTimeout() method is used to call a function or calculate an expression after a specified number of milliseconds.
Syntax
setTimeout(code,millisec)
Parameter Description
code Required. The string of JavaScript code to be executed after the function to be called.
millisec required. The number of milliseconds to wait before executing code.
Tips and Notes
Tip: setTimeout() only executes code once. If you want to call it multiple times, use setInterval() or have the code itself call setTimeout() again.
In this example, a prompt box will pop up 5 seconds after you click the button:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

2, HTML DOM clearTimeout( ) Method
Definition and Usage The clearTimeout() method can cancel the timeout set by the setTimeout() method. Syntax clearTimeout(id_of_settimeout)
Parameter Description
id_of_setinterval The ID value returned by setTimeout(). This value identifies the deferred execution code block to be canceled.
Example The following example calls the timedCount() function once every second. You can also use a button to terminate this scheduled message:
[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
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