1. Call setTimeout() after timeout
The setTimeout() method accepts two parameters, the first parameter is the function, the second parameter is the time (in microseconds), and returns the numerical ID
setTimeout( function () {
alert("Hello!");
},1000);
Cancel clearTimeout() before calling, accept a parameter timeout call ID
var timeOutId = setTimeout( function () {
alert("Hello!");
},1000);
clearTimeout(timeOutId);
2. Intermittently call setInterval()
The setInterval() method accepts two parameters, the first parameter is a function, and the second parameter is time (unit: microseconds ), return the numerical ID
setInterval( function () {
alert("Hello!");
},1000);
Cancel the call to clearInterval(), accept a parameter intermittent call ID
var intervalId = null;
var span = document.createElement("span"); // Create span node
span.Id="time"; //Set span id
document.body.appendChild(span); //Add span in body
function incrementNumber () {
var now = new Date();
var timeStr = now.toLocaleTimeString();
span.innerText =timeStr;
num ;
if (num == 10) {
clearInterval(intervalId ); //The time will remain unchanged after ten seconds
}
}
intervalId = setInterval(incrementNumber,1000);
3. Try to use timeout calls instead of intermittent calls
var num = 0;
var max = 10;
function incrementNumber() {
num ;
if (numsetTimeout(incrementNumber,1000);
} else {
alert("OK");
}
}
setTimeout(incrementNumber,1000);
Statement: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