js随机点名 js实现随机点名小功能
蒲小若 人气:0想了解js实现随机点名小功能的相关内容吗,蒲小若在本文为您仔细讲解js随机点名的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:js,随机点名,下面大家一起来学习吧。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="name1"></div> <button id="btn">stop</button> </body> <script type="text/javascript"> var flag = true; var str = "张三,李四,王五,马六"; var arr = str.split(","); var interId = setInterval("randName()",100); function randName(){ var rand = Math.floor(Math.random() * arr.length); name1.innerHTML = arr[rand]; } btn.onclick = function (){ if (flag) { btn.innerHTML = "start"; clearInterval(interId); flag = false; } else{ btn.innerHTML = "stop"; interId = setInterval("randName()",100); flag = true; } } </script> </html>
加载全部内容