计算机知识 - 计算机基础知识 - 电脑教程 - 电脑知识 - 电脑基础知识 加入收藏 | 设为首页 | 网站地图 | RSS
包罗万象网
您当前的位置:首页 > 网站建设 > javascript

面向对象的javascript方法写倒计时

时间:2012-01-28 18:43:26  来源:网站建设  作者:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <div id="time"></div>
  9. <script src="time.js"></script>
  10. <script>
  11. var test = new Countdown("time","2012/1/30,12:20:12");
  12. test.SetTime();
  13. </script>
  14. </body>
  15. </html>

 

  1. // JavaScript Document
  2. /**
  3. × @author tugenhua
  4. * javascript倒计时
  5. * elem 元素的ID nowTime 现在的时间 endTime 结束的时间
  6. */
  7. function Countdown(elem,endTime){
  8. this.elem = elem;
  9. this.nowTime = new Date().getTime();
  10. this.endTime = new Date(endTime).getTime();
  11. this.getID = document.getElementById(this.elem);
  12. this.reg = /\s+/g; //正则去掉空白/全局
  13. }
  14. Countdown.prototype = {
  15. SetTime : function(){
  16. var that = this;
  17. t = setInterval(function(){
  18. that.DownTime();
  19. },1000)
  20. },
  21. zero : function(n){
  22. var n = parseInt(n,10);
  23. if(n>0){
  24. if(n<10){
  25. n = "0" +n;
  26. }
  27. return String(n);
  28. }else{
  29. return "00";
  30. }
  31. },
  32. DownTime : function(){
  33. var space = parseInt((this.endTime - this.nowTime)/1000);
  34. this.nowTime+=1000;
  35. var d,h,m,s;
  36. var isboolean = true;
  37. if(space<=0){
  38. isboolean = false;
  39. }
  40. if(isboolean == true){
  41. d = this.zero(parseInt(space/3600/24)) + "天";
  42. h = this.zero(parseInt((space/3600)%24)) + "时";
  43. m = this.zero(parseInt((space/60)%60)) + "分";
  44. s = this.zero(parseInt(space%60)) + "秒";
  45. }else{
  46. d = 0 + "天";
  47. h = 0 + "时";
  48. m = 0 + "分";
  49. s = 0 + "秒";
  50. }
  51. total = d + h + m + s;
  52. this.getID.innerHTML = total.replace(this.reg,"");
  53. }
  54. }
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
相关文章
栏目更新
栏目热门