时间:2021-07-01 10:21:17 帮助过:9人阅读
本文实例为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。