js倒计时

AndyYang| 阅读:1041 发表时间:2011-11-28 04:57:00 js
摘要:这是我随便写的,实现倒计时,然后把花费的时间记录在影藏域中。因为我对js不熟,我今天刚开始找了很多,发现在倒计时的都可以出现负数,简直是误人子弟。各位,一定要加clearInterval(timmer)啊..
  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  4. <script src="jquery-1.5.1.js" language="javascript"></script>
  5. <script language="JavaScript">
  6. var TotalMilliSeconds = 5*1000;
  7. $(document).ready(function() {
  8. timmer = setInterval("takeCount()",1000);
  9. $('.button').click(function() {
  10. $('#ceshi').attr("value",timmer);
  11. clearInterval(timmer);
  12. });
  13. });
  14.  
  15. function takeCount()
  16. {
  17. //计数减一
  18. TotalMilliSeconds -= 1000;
  19. if(TotalMilliSeconds == 0) {
  20. clearInterval(timmer);
  21. alert('time over');
  22. }
  23. //计算时分秒
  24. var hours = Math.floor( TotalMilliSeconds / ( 1000 * 60 * 60 )) % 24;
  25. var minutes = Math.floor(TotalMilliSeconds / (1000 * 60)) % 60;
  26. var seconds = Math.floor(TotalMilliSeconds / 1000) % 60;
  27. //将时分秒插入到html中
  28. document.getElementById("RemainH").innerHTML = hours;
  29. document.getElementById("RemainM").innerHTML = minutes;
  30. document.getElementById("RemainS").innerHTML = seconds;
  31. $('#jquery').html(seconds);
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <div id="CountMsg">
  37. 倒计时还有:
  38. <strong id="RemainD"></strong><strong id="RemainH">XX</strong>
  39. <strong id="RemainM">XX</strong>
  40. <strong id="RemainS">XX</strong>
  41.  
  42. jquery
  43. <strong id="jquery">XX</strong>
  44. <input type="hidden" id="ceshi" value="">
  45. <input type="button" value="停止" class="button">
  46. </div>
  47. </body>
  48. </html>

 这是我随便写的,实现倒计时,然后把花费的时间记录在影藏域中。因为我对js不熟,我今天刚开始找了很多,发现在倒计时的都可以出现负数,简直是误人子弟。各位,一定要加clearInterval(timmer)啊.

本文为AndyYang原创,转载请注明出处!
如果您觉得好,可以打赏作者:
如果您觉得累了,是否想看点美女养养眼:猛戳>>朋友帮
如果您觉得皮了,是否想来点神吐槽:猛戳>>iPhone查询中

已有0条评论

昵称:
邮箱:
iPhone查询中 - bbs.ipcxz.com 朋友帮 - www.pengyb.cn iPhone查询中 - bbs.ipcxz.com
反馈
微信订阅号