Defines a function that executes immediately
(function () {var ticts=function ticts () {this.ticts = {};
};
Ticts.prototype.createticts=function (ID, dealline) {var ticts=this;
var time=moment (Dealline). diff (Moment ()); var _ticts=this.ticts[id] = {dealine:dealline, id:id, Time:time, in
Terval:setinterval (function () {var t = null;
var d = null;
var h = null;
var m = null; var s = null;
JS default timestamp is milliseconds and needs to be converted into seconds t = _ticts.time/1000;
D = Math.floor (t/(24 * 3600));
h = Math.floor ((t-24 * 3600 * d)/3600);
m = Math.floor ((t-24 * 3600 * d-h * 3600)/60); s = Math.floor ((t-24 * 3600 * d-h * 3600-m * 60));
Here you can do a formatted processing, even do a millisecond page rendering, based on DOM operations, too many countdown together will cause page performance drop document.getElementById (id). InnerHTML = d + ' Day ' + H
+ ' hour ' + M + ' min ' + S + ' sec ';
_ticts.time-= 1000; if (_ticts.time < 0)
Ticts.deleteticts (ID);//Determine if expiration, automatically delete timer after expiration}, 1000}}; Ticts.prototype.deleteTicts = function (id) {clearinterval (this.ticts[id].interval);//Clear Timer method, need timer pointer as parameter to pass Interval Delete this.ticts[id];//Deletes a property in an object by deleting the method;
Creates a new Ticts object and places it in the window global function, so that the object window is accessible to the HTML page (or other JS file). Ticts=new ticts ();
})();
Usage:
The HTML page must first introduce moment to use my method above
<script src= "//cdn.bootcss.com/moment.js/2.17.1/moment.min.js" ></script>
Let's say it's in the HTML where the countdown is.
<div id= "AAA" ></div>
<div id= "Aaa1" ></div>
<div id= "Aaa2" ></div>
You can write JS on the page:
Ticts.createticts ("AAA", "2017-1-20 20:20:20");
Ticts.createticts ("Aaa1", "2017-1-22 20:20:20");
Ticts.createticts ("Aaa2", "2017-1-23 20:20:20");