Based on javascript, the countdown time is accurate to milliseconds for flash sales, while for javascript flash sales

Source: Internet
Author: User

Based on javascript, the countdown time is accurate to milliseconds for flash sales, while for javascript flash sales

This article shares with you how to implement a countdown to flash sales in javascript, accurate to milliseconds for your reference. The specific content is as follows:


The image below is the effect of the limited-time grab above.

Ii. knowledge needed to achieve the effect of time-limited snatching: Javascript Date () object
Date () returns the current Date and event
GetYear () returns the year. It is best to get the year.
GetFullYear () method (full format such as 2016)
GetMonth () returns the month value (starting from 0, + 1)
GetDay () returns the day of the week (0-6)
GetHours () return hours (0-23)
GetMinutes () returns the number of minutes (0-59)
GetSeconds () returns the number of seconds
GetTime () returns milliseconds
Of course, we may not use all of the above call methods, but we also need to look at your own needs. We will directly go to the Code:
1. HTML page code:
<P class = "left-time"> </p>
We put the countdown content in the <p> label with the class left-time.
2. JS script:

$ (Function () {function leftTime () {var endTime = new Date (", 12:00:00"); // End Time var curTime = new Date (); // current time var leftTime = parseInt (endTime. getTime ()-curTime. getTime ()/1000); // obtain the time difference // hour, minute, and second. The modulo operation var d = parseInt (leftTime/(60*60*24) is required )); 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 +" day "+ h +" Hour "+ m +" Minute "+ s + ". "+ ms +" seconds "; $ (". left-time "). text (txt); if (leftTime <= 0) {$ (". left-time "). text ("End of group buying") ;}}; leftTime (); var set = setInterval (leftTime, 100 );});

The preceding js implements a simple example of time-limited snatching. The parseInt () method is an integer, And the getTime () method converts the time to milliseconds. Apart from the parseInt () method, you can also use Math. floor () is replaced by the rounded down method.

Finally, remember not to forget the content to be displayed when the end of the if () Judgment time. Otherwise, there will be unnecessary bugs!

Related Article

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: 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.