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!