首页 > 其他 > 详细

setTimeout(), clearTimeout()

时间:2017-08-14 12:07:02      阅读:157      评论:0      收藏:0      [点我收藏+]

今天学了setTimeout(), clearTimeout() ,想到可以做一个计时器。先给自己马一下

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>计时器</title>
 6     <script>
 7 
 8     var num =0;
 9     var i;
10     function startCount(){
11         //文本框的值赋给num
12         document.getElementById(‘count‘).value =num;
13         num += 1;
14         i =setTimeout("startCount()",1000);
15     }
16 
17     function stopCount(){
18         clearTimeout(i);
19     }
20     </script>
21     
22 
23 </head>
24 <body>
25 <input type="text" id="count"/>
26 <input type="button" value="继续" onclick="startCount()"/>
27 <input type="button" value="暂停" onclick="stopCount()">
28 
29 </body>
30 </html>

 

有一点就是,刚开始进去必须点一下继续才开始计时,研究一下看看怎么解决吧。

 

 

还可以做购物车添加物件,数量增加的效果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车效果</title>
    <script>

    var num =1;
    function startCount(){
        //文本框的值赋给num
        document.getElementById(‘count‘).value =num;
        num += 1;
        setTimeout("startCount()",1000);
    }
  
    </script>
    

</head>
<body>
<input type="text" id="count"/>
<input type="button" value="添加" onclick="startCount()"/>

</body>
</html>

 

setTimeout(), clearTimeout()

原文:http://www.cnblogs.com/kido050313/p/7357022.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!