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:
I,
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!