To achieve a precise countdown to milliseconds based on JavaScript snapping _javascript tips

Source: Internet
Author: User

This article for you to share the JavaScript implementation countdown to rush, accurate to millisecond countdown, for your reference, specific content as follows

One, the effect chart

The picture below is the effect of a timed grab on a poly-cost

Second, the realization of the effect of a timed robbery need to use knowledge : Javascript date () object
Date () returns the current date and event
GetYear () return year to get the most useful
getFullYear () method to operate (full format such as 2016)
GetMonth () returns the month value (starting from 0, +1)
Getday () Back to day of the week (0-6)
GetHours () Back hours (0-23)
Getminutes () Back minutes (0-59)
Getseconds () returns the number of seconds
GetTime () returns the number of milliseconds
Of course, the above method of call we do not necessarily all use, also depends on your own needs, nonsense not to say, we directly on the code:
1, HTML page code:
<p class= "Left-time" ></p>
We put the countdown content in the class for the Left-time <p> tag.
2, JS script:

$ (function () {
  function lefttime () {
    var endtime = new Date ("2016/5/20,12:00:00");//End time
    var curtime = new Date ()//Current time
    var lefttime = parseint ((Endtime.gettime ()-curtime.gettime ())/1000)/
    /hours, minutes, seconds require modulo operation
    var d = parseint (lefttime/(60*60*24));
    var h = parseint (lefttime/(60*60)%24);
    var m = parseint (lefttime/60%60);
    var s = parseint (lefttime%60);
    var ms = parseint (((Endtime.gettime ()-Curtime.gettime ())/100)%10);
    var txt = "Remaining:" +d+ "Days" +h+ "hours" +m+ "Minutes" +s+ "." +ms+ "seconds";
    $ (". Left-time"). Text (TXT);
    if (lefttime<=0) {$ (". Left-time"). Text ("group End");
  Lefttime ();
  var set = SetInterval (lefttime,100);
});

The above JS implementation of a simple time grab small example, where the parseint () method is to take the whole, getTime () to convert the time to milliseconds, in addition to the parseint () method, you can use the Math.floor () down the whole method instead.

Finally remember don't forget to give an if () to determine the time at the end of the need to display the content Oh, otherwise there will be unnecessary small bugs yo!

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.