JS Simple countdown (code optimization)

Source: Internet
Author: User
Tags local time

Countdown online A lot, so there is nothing to say, support: 1. Years, months, days, days, hours, minutes, minutes, etc. countdown.

The JS code is as follows:

/** JS Simple countdown * @param {date,obj} Date Object Format*/ functionCountdown (date,obj) {varSelf = This; Self.date=date; Self.obj=obj; Self._init (); }; $.extend (countdown.prototype,{_init:function(){        varSelf = This, obj=Self.obj; varDateTime =Self._calculatetime (); if(OBJ.SEC) {$ (obj.sec). HTML (DATETIME.SEC); }        if(Obj.mini) {$ (Obj.mini). HTML (Datetime.mini); }        if(Obj.hour) {$ (obj.hour). HTML (datetime.hour); }        if(Obj.day) {$ (obj.day). HTML (datetime.day); }        if(Obj.month) {$ (obj.month). HTML (datetime.month); }        if(obj.year) {$ (obj.year). HTML (datetime.year); }        //SetInterval will have an error of about 5 seconds per hour, so use settimeout to make the timer more accurate .T &&cleartimeout (t); vart = setTimeout (function() {self._init (); }, 1000); }, _zero:function(n) {varn = parseint (n,10); if(N > 0) {            if(N < 10) {n= "0" +N; }            returnString (n); }Else {            returnn = "00"; }}, _calculatetime:function(){        varSelf = This, Date= Self.date | | DATE.UTC (2050, 0, 1); varEnd =NewDate (date), now=NewDate (); //the getTimezoneOffset () method returns the difference between GMT and local time, measured in minutes.         varLefttime = Math.Round ((End.gettime ()-Now.gettime ())/1000) + end.gettimezoneoffset () * 60;varobj ={sec:"00", Mini:"00", hour:"00", Day:"00", Month:"00", Year:"0"        }; if(Lefttime > 0) {obj.sec= Self._zero (lefttime% 60); Obj.mini= Math.floor (LEFTTIME/60) > 0? Self._zero (Math.floor (LEFTTIME/60)% 60): "00"; Obj.hour= Math.floor (lefttime/3600) > 0?            Self._zero (Math.floor (lefttime/3600)% 24): "00"; Obj.day = Math.floor (Lefttime/(3600)) > 0? Self._zero (Math.floor (Lefttime/(24*3600))% 30): "00"; Obj.month= Math.floor (Lefttime/(* 3600)) > 0? Self._zero (Math.floor (Lefttime/(30 * 24 * 3600))% 12): "00"; //year, by the 360-day second of the regression yearObj.year = Math.floor (Lefttime/(* 3600)) > 0? Math.floor (Lefttime/(360 * 30 * 24 * 3600)): "0"; }        returnobj; } });

If the HTML is as follows:

<class= "Hour"></span><   class= "Mini"></span><   class= "SEC"></span> sec

So we can initialize the code like this:

var d = 1419509471000;     // var d = DATE.UTC (2030, 6, +, +); var obj = {    sec: $ (". Sec"),    Mini: $(". Mini"),     Hour: $ (". Hour") )}new countdown (d, obj);

Jsfiddler Countdown Demo

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.