Javascript 时间 倒计时

时间:2022-03-15 22:08:44

更新一下时间倒计时插件(jQUery)

 1 $.fn.genTimer = function (e) {
 2     function u(e) {
 3     var t = Math.floor(e / n),
 4         r = Math.floor((e - t * n) / 36e5),
 5         i = Math.floor((e - t * n - r * 1e3 * 60 * 60) / 6e4),
 6         s = Math.floor((e - t * n - r * 1e3 * 60 * 60 - i * 1e3 * 60) / 1e3);
 7     return {
 8         hours: ("0" + r).slice(-2),
 9         minutes: ("0" + i).slice(-2),
10         seconds: ("0" + s).slice(-2),
11         dates: t
12     }
13     }
14     if (this.data("handled")) return this;
15     this.data("handled", !0);
16     var t = {
17     beginTime: new Date,
18     day_label: "day",
19     days_label: "days",
20     unitWord: {
21         hours: ":",
22         minutes: ":",
23         seconds: ""
24     },
25     type: "",
26     callbackOnlyDatas: !1
27     },
28     n = 864e5,
29     r = $.extend({}, t, e),
30     i = this;
31     r.targetTime = r.targetTime.replace(/\-/g, "/");
32     r.targetTime = isNaN(r.targetTime) ? r.targetTime : parseInt(r.targetTime);37     var s = new Date(r.targetTime) - new Date(r.beginTime),
38     o = function () {
39         if (s < 0) {
40         r.callback.call(i, r.callbackOnlyDatas ? {
41             hours: "00",
42             minutes: "00",
43             seconds: "00",
44             dates: 0
45         } : "00" + r.unitWord.hours + "00" + r.unitWord.minutes + "00");
46         clearInterval(i.interval)
47         } else {
48         var e = u(s);
49         if (r.callbackOnlyDatas) r.callback.call(i, e);
50         else if (r.type == "day") {
51             s >= n * 2 ?
52             r.callback.call(i, '<span class="day_count">' + e.dates + '</span><span class="day">' + r.days_label + '</span><span class="day_seconds">' + e.hours + r.unitWord.hours + e.minutes + r.unitWord.minutes + e.seconds + r.unitWord.seconds + "</span>")
53             : (
54             s >= n ?
55             r.callback.call(i, '<span class="day_count">' + e.dates + '</span><span class="day">' + r.day_label + '</span><span class="day_seconds">' + e.hours + r.unitWord.hours + e.minutes + r.unitWord.minutes + e.seconds + r.unitWord.seconds + "</span>")
56             : r.callback.call(i, '<span class="seconds">' + e.hours + r.unitWord.hours + e.minutes + r.unitWord.minutes + e.seconds + r.unitWord.seconds + "</span>")
57             );
58         }
59         else if (r.type == "diffNoDay") {
60             var t = e.hours;
61             s >= n && (t = Number(e.dates * 24) + Number(e.hours));
62             r.callback.call(i, '<span class="hours">' + t + '</span><span class="miniutes">' + r.unitWord.hours + e.minutes + '</span><span class="senconds">' + r.unitWord.minutes + e.seconds + r.unitWord.seconds + "</span>")
63         } else {
64             var t = e.hours;
65             s >= n && (t = Number(e.dates * 24) + Number(e.hours));
66             r.callback.call(i, '<span class="seconds">' + t + r.unitWord.hours + e.minutes + r.unitWord.minutes + e.seconds + r.unitWord.seconds + "</span>")
67         }
68         }
69         s -= 1e3
70     };
71     i.interval = setInterval(o, 1e3);
72     o();
73     return this
74 };

 

调用:

1 $(this).genTimer({
2     beginTime: new Date(),//开始时间
3     targetTime: new Date(),//结束时间
4     type: "day",
5     callback: function (e) {
6         this.html(e)
7     }
8 })

 

附加:一个C#获取现在到1970-1-1毫秒算法

1  string times = (DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds - (8 * 60 * 60 * 1000)).ToString("F0");