7_BOM定时器.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 定时器 属于BOM对象下的方法
  11. // 在指定间隔时间后自动执行
  12. // 只执行一次的定时器
  13. // window.setTimeout()
  14. // window 可以省略不写
  15. // setTimout 有两个参数 第一个是函数 匿名函数(到达指定时间后要做的事情) 第二个是时间 间隔的时间 毫秒单位
  16. setTimeout(function(){
  17. console.log("hello world");
  18. },2000);
  19. // 可以重复执行的定时器
  20. // window 可以省略
  21. // window.setInterval();
  22. // setInterval 有两个参数 第一个是函数 匿名函数(到达指定时间后要做的事情) 第二个是时间 间隔的时间 毫秒单位
  23. // setInterval 每个间隔时间后重复执行
  24. setInterval(function(){
  25. console.log("hello world");
  26. },1000);
  27. // 清除定时器 结束定时器的执行
  28. // clearTimeout() 用来清除 setTimeout() 方法设置的定时器
  29. // clearInterval() 用来清除 setInterval() 方法设置的定时器
  30. // setTimeout 和 setInterval 都有返回值 返回定时器的唯一表示
  31. // var timer = setTimeout(function(){
  32. // console.log("hello");
  33. // },3000);
  34. // var timer2 = setTimeout(function(){
  35. // console.log("hello2");
  36. // },3000);
  37. // console.log(timer,timer2);
  38. // clearTimeout(timer);
  39. // clearTimeout(timer2);
  40. var timer = setInterval(function(){
  41. console.log("hello world");
  42. },1000);
  43. // 清除定时器
  44. clearInterval(timer);
  45. </script>
  46. </body>
  47. </html>