5_定时器.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(timer);
  26. },3000);
  27. // console.log(timer2);
  28. // function fn(){
  29. // console.log("hello world");
  30. // }
  31. // fn()
  32. // var fn = function(){
  33. // console.log("hello world");
  34. // }
  35. // fn()
  36. </script>
  37. </body>
  38. </html>