js随机点名

定时器案例。

 

 

<!--
Author: XiaoWen
Create a file: 2016-12-08 12:27:32
Last modified: 2016-12-08 12:51:59
Start to work:
Finish the work:
Other information:
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>随机点名</title>
</head>
<body>
  <div style="width:100px;height:100px;border:1px solid #ccc"></div>
  <input type="button" value="开始">
  <input type="button" value="停止">
  <script>
    var div=document.getElementsByTagName("div")[0];
    var arr=["","","","","","","","","",""]
    var a_ipt=document.getElementsByTagName("input");
    var inter; //定时器的名字,这里要写在外面,否则外面的函数不能找到
    a_ipt[0].onclick=function(){
      clearInterval(inter) //先清除定时器,不然定时器会累加
      inter=setInterval("fn1()",50); //为什么定时器里调用的函数名要加绰号?如果不加,浏览器看到 fn() 就直接执行 fn() 了。而看到被引号包围的 "fn()" 不会。
    }
    function fn1(){
      div.innerHTML=arr[parseInt(Math.random()*10)]
    }
    a_ipt[1].onclick=function(){
      clearInterval(inter)
    }
  </script>
</body>
</html>

 

posted @ 2016-12-08 13:42  程序媛李李李李蕾  阅读(673)  评论(0)    收藏  举报