首页 > 代码库 > JavaScript倒计时类
JavaScript倒计时类
(function (){ var jtimer = function() { // init if(arguments.length >= 1) { this.setEndTime(arguments[0]); } if(arguments.length >= 2) { this.setGenerateCallBack(arguments[1]); } }; jtimer.prototype.setEndTime = function () { if(arguments.length == 1) { this.endTime = arguments[0]; // Date } } jtimer.prototype.getMillisecond = function () { return this.endTime.getTime() - new Date().getTime(); }; jtimer.prototype.setGenerateCallBack = function (callback) { if(typeof callback == "undefined") return; this.generateCallBack = callback; } jtimer.prototype.generate = function () { if(typeof this.generateCallBack == "undefined") return; var ms = this.getMillisecond(); this.generateCallBack( Math.floor(ms/(1000 * 60 * 60 * 24)), Math.floor(ms/(1000*60*60)) % 24, Math.floor(ms/(1000*60)) % 60, Math.floor(ms/1000) % 60 ); }; jtimer.prototype.start = function () { var delay = 1000; if(arguments.length == 1) { delay = arguments[0]; } _this = this; // for closure this.interval = window.setInterval( function() { _this.generate(); }, delay); } jtimer.prototype.stop = function () { if(typeof this.interval == "undefined") return; window.clearInterval(this.interval); this.interval = undefined; } window.jtimer = jtimer;})();var jt = new jtimer(new Date("6/27/2016"), function (day, hour, min, sec) { console.log(day + "," + hour + "," + min + "," + sec);});jt.start(1000);
JavaScript倒计时类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。