javascript實現(xiàn)的一個隨機點名功能
更新時間:2014年08月26日 11:52:19 投稿:junjie
這篇文章主要介紹了JS實現(xiàn)的一個隨機點名功能,邏輯簡單,用在班級或活動時是個不錯的選擇,需要的朋友可以參考下
這個其實是對JS隨機數(shù)的一個練習(xí)方式,先把所有人得名字預(yù)先寫好在一個數(shù)組里,然后讓數(shù)組里的值快速的顯示在區(qū)域內(nèi),當(dāng)你按停的時候滾動就會停止達到隨機效果。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JS寫的隨機點名 - 瓊臺博客</title> <script type="text/javascript"> var isRun=true; var a = ["張三","李四","王五","趙六"]; var a2 = new Array(); function action(str){ var s = document.getElementById("bt").value; if(s=="開始"){ isRun=true; run(); document.getElementById("bt").value="結(jié)束"; }else{ isRun=false; document.getElementById("bt").value="開始"; } } function run(){ var i = Math.floor(Math.random() * a.length+ 1)-1; document.getElementById("show").innerHTML=a[i]; if(isRun==false){ var b =true; for(var j in a2){ if(a2[j]==i){ b=false; } } if(b){ a2[a2.length]=i; return; } } setTimeout("run()",10); } </script> </head> <body> <div style="text-align:center; margin-top:100px;width:100%;"> <div id="show" style="margin:auto;font-size:50px;width:100px;height:50px; background:#FFEEFF"></div> <div style="margin-top:20px;"> <input id="bt" type="button" onclick="action()" value="開始"/> </div> </div> </body> </html>
相關(guān)文章
JavaScript基礎(chǔ)語法、dom操作樹及document對象
本文介紹了學(xué)習(xí)javascript重要的3個內(nèi)容基礎(chǔ)語法、dom操作樹及document對象,小伙伴們一定要弄明白才行2014-12-12js 使用form表單select類實現(xiàn)級聯(lián)菜單效果
本文介紹javascript使用html中form表單中的select類實現(xiàn)級聯(lián)菜單效果,需要的朋友可以了解下2012-12-12一個簡單的網(wǎng)站訪問JS計數(shù)器 刷新1次加1次訪問
一個簡單的網(wǎng)站訪問JS計數(shù)器,一般就是學(xué)習(xí)下原來,不建議使用,現(xiàn)在cnzz或百度統(tǒng)計多試不錯的2012-09-09JavaScript中用于生成隨機數(shù)的Math.random()方法
這篇文章主要介紹了JavaScript中用于生成隨機數(shù)的Math.random()方法,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06javascript setTimeout和setInterval 的區(qū)別
window對象有兩個主要的定時方法,分別是setTimeout 和 setInteval 他們的語法基本上相同,但是完成的功能取有區(qū)別。2009-12-12