homework01.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. #tel {
  8. width: 500px;
  9. height: 100px;
  10. text-align: center;
  11. font: bolder 72px/100px Arial;
  12. margin: 50px auto;
  13. background-color: blue;
  14. color: yellow;
  15. }
  16. #buttons {
  17. width: 500px;
  18. margin: 0 auto;
  19. text-align: center;
  20. }
  21. #buttons > button {
  22. width: 120px;
  23. height: 40px;
  24. background-color: red;
  25. color: white;
  26. font: 28px/40px "微软雅黑";
  27. border: none;
  28. margin: 0 10px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <!-- 高内聚 低耦合 (high cohesion low coupling)-->
  34. <div id="tel">13012345678</div>
  35. <div id="buttons">
  36. <button id="startButton">开始</button>
  37. <button id="stopButton">停止</button>
  38. </div>
  39. <script>
  40. // window - BOM
  41. // - setTimeout / clearTimeout
  42. // - setInterval / clearInterval
  43. // - alert / prompt / confirm
  44. // - document / location / history / screen / navigator
  45. var tels = [];
  46. for (var i = 0; i < 100; i += 1) {
  47. tel = '13';
  48. for (var j = 0; j < 9; j += 1) {
  49. tel += parseInt(Math.random() * 10);
  50. }
  51. tels[i] = tel;
  52. }
  53. var startButton = document.getElementById('startButton');
  54. var stopButton = document.getElementById('stopButton');
  55. // 给标签绑定事件回调(callback)函数
  56. // 我们知识事件发生时要做什么但我们不知道事件什么时候发生
  57. // 在这种场景我们就需要提供一个回调函数(不是自己调用而是留给他人调用)
  58. var timerId = 0;
  59. var luckyTel = '';
  60. startButton.onclick = function() {
  61. duration = parseInt(Math.random() * 950 + 50);
  62. timerId = setTimeout(function() {
  63. var index = parseInt(Math.random() * tels.length);
  64. luckyTel = tels[index];
  65. displayTel = luckyTel.substring(0, 3) + '****' +
  66. luckyTel.substring(7, 11)
  67. document.getElementById('tel').textContent = displayTel;
  68. duration = parseInt(Math.random() * 950 + 50);
  69. timerId = setTimeout(arguments.callee, duration);
  70. }, duration);
  71. };
  72. stopButton.onclick = function() {
  73. clearInterval(timerId);
  74. alert('被抽中的幸运号码是: ' + luckyTel);
  75. };
  76. </script>
  77. </body>
  78. </html>