3.window.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. <style>
  8. #btn1 {
  9. width: 180px;
  10. padding: 20px;
  11. background: #f00;
  12. color: #ff0;
  13. text-align: center;
  14. }
  15. #btn2 {
  16. width: 180px;
  17. padding: 20px;
  18. background: #00f;
  19. color: #0f0;
  20. text-align: center;
  21. margin-top: 20px;
  22. }
  23. #btn3 {
  24. width: 180px;
  25. padding: 20px;
  26. background: #f00;
  27. color: #ff0;
  28. text-align: center;
  29. margin-top: 40px;
  30. }
  31. #btn4 {
  32. width: 180px;
  33. padding: 20px;
  34. background: #00f;
  35. color: #0f0;
  36. text-align: center;
  37. margin-top: 20px;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div id="btn1">这是一个开始按钮</div>
  43. <div id="btn2">这是一个结束按钮</div>
  44. <div id="btn3">这是一个新的按钮</div>
  45. <div id="btn4">这是一个旧的按钮</div>
  46. <!--
  47. setInterval 定时器 规定时间内执行函数 setInterval(函数,时间)
  48. clearInterval 清除定时器
  49. setTimeout 延时器 规定时间后执行一次函数 setTimeout(函数,时间)
  50. -->
  51. <script>
  52. // window.alert() => alert()
  53. // window.alert("警告框");
  54. // window.confirm() => confirm()
  55. // window.confirm("确认框");
  56. // window.prompt() => prompt()
  57. // window.prompt("提示框");
  58. var timer1;
  59. // 获取按钮
  60. var btn1 = document.getElementById("btn1");
  61. btn1.onclick = function() {
  62. timer1 = setInterval(function(){
  63. console.log("执行一次")
  64. },500)
  65. }
  66. var btn2 = document.getElementById("btn2");
  67. btn2.onclick = function() {
  68. clearInterval(timer1)
  69. }
  70. var timer2;
  71. var btn3 = document.getElementById("btn3");
  72. btn3.onclick = function() {
  73. timer2 = setTimeout(function(){
  74. console.log("这是新的按钮");
  75. },3000)
  76. }
  77. var btn4 = document.getElementById("btn4");
  78. btn4.onclick = function() {
  79. clearTimeout(timer2);
  80. alert("成功了");
  81. }
  82. </script>
  83. </body>
  84. </html>