原生js實(shí)現(xiàn)隨機(jī)點(diǎn)名
本文實(shí)例為大家分享了js實(shí)現(xiàn)隨機(jī)點(diǎn)名的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>隨機(jī)點(diǎn)名</title></head><body> <h1 id='names'>點(diǎn)名</h1> <input type='button' value='開始' id=start_btn> <input type='button' value='結(jié)束' id=stop_btn disabled> <script>'../DOM/tools.js'</script> <script> let arrName = ['肖巍', '楊恩', '小菊花', '二狗', '小強(qiáng)', '小????', '彎彎姐', '陳鍇', '徐陽', '陳陽吉', '張勝江', '我', '波哥', '阿湯哥', '杰哥', '薛老板']; let names = document.querySelector('h1'); let start_btn = document.querySelector('#start_btn'); let stop_btn = document.querySelector('#stop_btn'); let i, timer; function creatRandom(min, max) { if (!max) { [max, min] = [min, 0]; } let number = parseInt(Math.random() * (max - min + 1) + min); return number; } start_btn.onclick = function () { stop_btn.disabled ? stop_btn.disabled = false : stop_btn.disabled = true; start_btn.disabled ? start_btn.disabled = false : start_btn.disabled = true; timer = setInterval(() => { i = creatRandom(0, arrName.length - 1); names.innerHTML = arrName[i]; }, 100); }; stop_btn.onclick = function () { stop_btn.disabled ? stop_btn.disabled = false : stop_btn.disabled = true; start_btn.disabled ? start_btn.disabled = false : start_btn.disabled = true; console.log(arrName [i]); clearInterval(timer); arrName.splice(i, 1); if(!arrName.length){ start_btn.disabled=true; stop_btn.disabled=true; names.innerHTML = '抽獎完畢'; } }; </script></body></html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTTP協(xié)議常用的請求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))2. HTML DOM setInterval和clearInterval方法案例詳解3. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)4. React優(yōu)雅的封裝SvgIcon組件示例5. HTML5 Canvas繪制圖形從入門到精通6. XML入門的常見問題(三)7. Vue如何使用ElementUI對表單元素進(jìn)行自定義校驗(yàn)及踩坑8. CSS清除浮動方法匯總9. XML在語音合成中的應(yīng)用10. 不要在HTML中濫用div
