Own encapsulation of a simple countdown function example _javascript tips

Source: Internet
Author: User

Because the usual work is very commonly used to this function, so use this national day holiday, to comb and the original code to improve, and then integrated a common function, and finally encapsulated this "simple Countdown" function.

The countdown method has the following features:

1. Match the current computer time according to the specified date

2. Set the countdown for different time periods within each day by specifying an array parameter.

* The method has not been detected by the actual work, the stability is unknown (if the actual work through, will delete this paragraph)

function Countdown (date,target,filter) {var settime = new Date (date). GetTime (), timer = null;

    function core () {var nowtime = new Date (). GetTime (), Lefttime = 0, d = 0,h = 0,m = 0,s = 0;
      Conditation @ doublue time if (filter.length) {settime = new Date ();
        for (Var i=0,l=filter.length;i<l;i++) {settime.sethours (filter[i]);
        Settime.setminutes (0);
        Settime.setseconds (0);
        if (Nowtime < Settime.gettime ()) {break;
          }else if (i = = filter.length-1) {settime.setdate (Settime.getdate () +1);
        Settime.sethours (Filter[0]);
    }}////////////////////////////Lefttime = Math.ceil ((settime-nowtime)/1000);
      if (nowtime <= settime) {d = ~ ~ (lefttime/86400);
      H = ~ ~ (lefttime%86400/3600);
      m = ~ ~ (LEFTTIME%86400%3600/60);
      s = ~ ~ (lefttime%86400%3600%60);
    Timer = settimeout (CORE,1E3); }else{cleartimeout (timer);
    timer = null;
    
  //here set out format target.innerhtml = ' d: ' +d+ ' H: ' +h+ ' m: ' +m+ ' s: ' +s;
Core (); }

Call Mode:

Ordinary Call
countdown (' 2016/10/02/23:43 ', odiv);
Specify Time Cycle Countdown
countdown (' 2016/10/02/23:43 ', odiv,[9,11,18]);

The above of their own encapsulation of a simple countdown function example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.