首页 > 其他 > 详细

倒计时例子

时间:2017-08-21 11:53:29      阅读:259      评论:0      收藏:0      [点我收藏+]

效果图:

技术分享

技术分享

代码:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>setTimeout倒计时</title>
 6     <script>
 7     var i = 5;
 8     var t;
 9     function startCount(){ 
10         if (i<0) {
11            stop();
12        }else{
13          document.getElementById("text").value = i;
14          t=setTimeout("startCount()",1000);
15          i =i-1; 
16        }    
17     }
18 
19     function stop(){
20            clearTimeout(t);
21            alert("游戏结束");    
22     }
23     
24     </script>
25 </head>
26 <body>
27 <input type="text" id="text">
28 <input type="button" id="start" onclick="startCount()" value="开始">    
29 </body>
30 </html>

注意:if判断语句要放在前面,赋值语句要放在自减前面,因为执行顺序问题,所以代码顺序不能乱,乱了就各种问题,得不到想要的效果。

 

倒计时例子

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

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