<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>nihao</title> <style> input{ background-color: greenyellow; border: 1px solid green; width:100px; height:50px; font-size: 20px; font-family: "微軟雅黑"; font-weight: bold; } </style> </head> <body> <center> <h3 color="red">恭喜您:</h3> <div style="font-size: 20px;text-align: center;border: 2px solid green;width:200px;height: 50px;line-height: 50px;"> <span id="sp"></span> </div> <hr /> <input type="button" value="開始抽獎" id="start"/> </center> <script> /*定義數組存放參與抽的人員名單*/ var arr=["小花","小明","小紅","小麗","小強","小白","小黑","小剛"]; function fun() { /*隨機獲取數組人名*/ var index=Math.floor(Math.random() * arr.length); var name=document.getElementById("sp"); name.innerHTML=arr[index]+" 中獎了"; } //點擊開始按鈕,定時器循環執行 var start=document.getElementById("start"); var time_ID=null; start.οnclick=function () { if(time_ID==null){ time_ID=setInterval(fun,50); start.value="結束抽獎"; }else { clearInterval(time_ID); start.value="開始抽獎"; time_ID=null; } }; </script> </body> </html>