javascript實(shí)現(xiàn)的一個(gè)隨機(jī)點(diǎn)名功能
這個(gè)其實(shí)是對(duì)JS隨機(jī)數(shù)的一個(gè)練習(xí)方式,先把所有人得名字預(yù)先寫好在一個(gè)數(shù)組里,然后讓數(shù)組里的值快速的顯示在區(qū)域內(nèi),當(dāng)你按停的時(shí)候滾動(dòng)就會(huì)停止達(dá)到隨機(jī)效果。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JS寫的隨機(jī)點(diǎn)名 - 瓊臺(tái)博客</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對(duì)象
本文介紹了學(xué)習(xí)javascript重要的3個(gè)內(nèi)容基礎(chǔ)語法、dom操作樹及document對(duì)象,小伙伴們一定要弄明白才行2014-12-12js 使用form表單select類實(shí)現(xiàn)級(jí)聯(lián)菜單效果
本文介紹javascript使用html中form表單中的select類實(shí)現(xiàn)級(jí)聯(lián)菜單效果,需要的朋友可以了解下2012-12-12一個(gè)簡單的網(wǎng)站訪問JS計(jì)數(shù)器 刷新1次加1次訪問
一個(gè)簡單的網(wǎng)站訪問JS計(jì)數(shù)器,一般就是學(xué)習(xí)下原來,不建議使用,現(xiàn)在cnzz或百度統(tǒng)計(jì)多試不錯(cuò)的2012-09-09JavaScript中用于生成隨機(jī)數(shù)的Math.random()方法
這篇文章主要介紹了JavaScript中用于生成隨機(jī)數(shù)的Math.random()方法,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06javascript setTimeout和setInterval 的區(qū)別
window對(duì)象有兩個(gè)主要的定時(shí)方法,分別是setTimeout 和 setInteval 他們的語法基本上相同,但是完成的功能取有區(qū)別。2009-12-12