setInterval(“函数”,时间t)是指执行了函数时间t之后再次执行函数(无限次执行)
setTimeout(“函数”,时间t)是指时间t之后执行函数(仅执行一次)
setInterval代码演示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="开始" onclick="start()"/>
<input type="button" value="停止" onclick="stop()"/>
<script>
function t(){
console.log(123);
}
var id = setInterval("t()",1000)//执行t()函数之后过了1秒继续再次执行
function start(){
id = setInterval("t()",1000)
}
function stop(){
clearInterval(id);//停止执行指令id是id的指令
}
</script>
</body>
</html>
setTimeout代码演示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="开始" onclick="start()"/>
<input type="button" value="停止" onclick="stop()"/>
<script>
function t(){
console.log(123);
}
var id = setTimeout("t()",1000);//1秒后执行t()函数
clearTimeout(id);//停止执行指令id为id的指令
</script>
</body>
</html>