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