1、setInterval ():setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
function jump(){
window.location.href="http://www.baidu.com";
}
</script>
<input type="button" value="查询" onclick="jump()"/>
<input type="button" value="停止" onclick="c()"/>
<input type="button" value="开始" onclick="s()"/>
<script>
function t(){
console.log(11);
}
var id=setInterval("t()",1000);
function s(){
id=setInterval("t()",1000);
}
function c(){
clearInterval(id);
}
</script>
</body>
</html>
2、setTimeout():setTimeout() 只执行 code 一次,如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout()
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
function jump(){
window.location.href="http://www.baidu.com";
}
</script>
<input type="button" value="查询" onclick="jump()"/>
<input type="button" value="停止" onclick="c()"/>
<input type="button" value="开始" onclick="s()"/>
<script>
function t(){
console.log(11);
}
var id=setTimeout("t()",1000);
function s(){
id=setTimeout("t()",1000);
}
function c(){
clearTimeout(id);
}
</script>
</body>
</html>