<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//setInterval(); setTimeout();
// var num = 0;
// setInterval(function () {
// console.log(num);
// num++;
// },1000);
//用途:setInterval()循环定时器;周而复始的执行(循环执行)
//用途:setTimeout() 炸弹定时器;用完以后立刻报废(只执行一次)
//定义方法1(匿名函数)
// setInterval(function () {
// console.log(1);
// },1000);
// //定义方法2
// setInterval(fn,1000);
// function fn(){
// console.log(2);
// }
//定义方法3
// setInterval("fn(3)",1000);
// function fn(aaa){
// console.log(aaa);
// }
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//返回值,清除定时器。
var num = 1;
//setInterval他的返回值就是定时器的名字
var timer = setInterval(function () {
console.log(num);
num++
if(num===10){
//如何停止定时器呢???
clearInterval(timer);
}
},500);
</script>
</body>
</html>
![](https://img.haomeiwen.com/i13877849/6edd31896430fc36.jpg)
setInterval
网友评论