5_定时器.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // setTimeout(function(){},1000);
  11. // 接收两个参数,第一个参数是函数 (回调函数),第二个参数是时间 毫秒为单位
  12. // 只能执行一次
  13. // setTimeout(function(){
  14. // console.log("hello world");
  15. // },3000);
  16. // setInterval(function(){},1000);
  17. // 接收两个参数,第一个参数是函数 (回调函数),第二个参数是时间 毫秒为单位
  18. // 可以执行多次
  19. // 有返回值 返回值为一个数字 代表定时器的id
  20. var timer = setInterval(function(){
  21. console.log("hello world");
  22. },1000);
  23. console.log(timer);
  24. setTimeout(function(){
  25. // 清除定时器 clearInterval
  26. clearInterval(timer);
  27. },3000);
  28. //清除定时器 clearTimeout
  29. clearTimeout();
  30. // console.log(timer2);
  31. // function fn(){
  32. // console.log("hello world");
  33. // }
  34. // fn()
  35. // var fn = function(){
  36. // console.log("hello world");
  37. // }
  38. // fn()
  39. </script>
  40. </body>
  41. </html>