The countdown based on moment

Source: Internet
Author: User
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");


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.