12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // 定时器 属于BOM对象下的方法
- // 在指定间隔时间后自动执行
- // 只执行一次的定时器
- // window.setTimeout()
- // window 可以省略不写
- // setTimout 有两个参数 第一个是函数 匿名函数(到达指定时间后要做的事情) 第二个是时间 间隔的时间 毫秒单位
- setTimeout(function(){
- console.log("hello world");
- },2000);
- // 可以重复执行的定时器
- // window 可以省略
- // window.setInterval();
- // setInterval 有两个参数 第一个是函数 匿名函数(到达指定时间后要做的事情) 第二个是时间 间隔的时间 毫秒单位
- // setInterval 每个间隔时间后重复执行
- setInterval(function(){
- console.log("hello world");
- },1000);
- // 清除定时器 结束定时器的执行
- // clearTimeout() 用来清除 setTimeout() 方法设置的定时器
- // clearInterval() 用来清除 setInterval() 方法设置的定时器
- // setTimeout 和 setInterval 都有返回值 返回定时器的唯一表示
- // var timer = setTimeout(function(){
- // console.log("hello");
- // },3000);
- // var timer2 = setTimeout(function(){
- // console.log("hello2");
- // },3000);
- // console.log(timer,timer2);
- // clearTimeout(timer);
- // clearTimeout(timer2);
- var timer = setInterval(function(){
- console.log("hello world");
- },1000);
- // 清除定时器
- clearInterval(timer);
- </script>
- </body>
- </html>
|