<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/jquery.min.js"></script> </head> <body> <div id="p1"></div> <div id="p2"></div> <script> var i = 0; function DownTime(StartTime, EndTime, CurrentTime, callback) { //计算器 this.couputer = function(StatTime, EndTime, type) { var EndTime = new Date(EndTime); var NowTime = new Date(StatTime); NowTime = new Date(NowTime.getTime() + 1000 * i); var t = EndTime.getTime() - NowTime.getTime(); var d = 0; var h = 0; var m = 0; var s = 0; if(t >= 0) { d = Math.floor(t / 1000 / 60 / 60 / 24); h = Math.floor(t / 1000 / 60 / 60 % 24); m = Math.floor(t / 1000 / 60 % 60); s = Math.floor(t / 1000 % 60); } var Time = { d: d, h: h, m: m, s: s } if(type == 0) { Time.title = ‘out‘; // clearTimeout(Timer) } else if(type == 1) { Time.title = ‘wait‘; } else if(type == 2) { Time.title = ‘end‘; } var time = d + "天" + h + "时" + m + "分" + s + "秒" + Time.title; callback(Time); }; var self = this; this.init = function(StartTime, EndTime, CurrentTime) { if(self.judge1(StartTime, CurrentTime)) { //距离抢购时间倒计时 self.couputer(CurrentTime, StartTime, 1); } else { if(self.judge2(CurrentTime, EndTime)) { //距离抢购时间倒计时 self.couputer(CurrentTime, EndTime, 2); } else { //结束抢购 self.couputer(CurrentTime, EndTime, 0); } } } this.init(StartTime, EndTime, CurrentTime); } //活动结束 DownTime.prototype.judge1 = function(StartTime, CurrentTime) { StartTime = new Date(StartTime.replace(/\-/g, "\/")); CurrentTime = new Date(CurrentTime.replace(/\-/g, "\/")); CurrentTime = new Date(CurrentTime.getTime() + 1000 * i); if(StartTime > CurrentTime) { return true; } return false; } //开始抢购 DownTime.prototype.judge2 = function(CurrentTime, EndTime) { CurrentTime = new Date(CurrentTime.replace(/\-/g, "\/")); EndTime = new Date(EndTime.replace(/\-/g, "\/")); CurrentTime = new Date(CurrentTime.getTime() + 1000 * i); if(EndTime > CurrentTime) { return true; } return false; } function GetBuying() { new DownTime("2017/12/22 15:00:08", "2017/12/22 17:00:10", "2017/12/22 16:59:58", function(ret) { console.log(ret) }); new DownTime("2017/12/22 17:00:08", "2017/12/22 22:00:10", "2017/12/22 16:59:58", function() { }); i++; } var Timer = setInterval(GetBuying, 1000) </script> </body> </html>
拿走不谢