Below is a brief introduction to three different countdown methods.
Let's take a look at one of the gettime () methods used. This time refers to the time from midnight 1970 to the present. Well, knowing this, it's a good idea.
Take a look at the code
<script>function Fresh () {varover ="2016/12/11,20:00:00"; varEndtime =NewDate (over); varNowtime =NewDate (); varLeftsecond = parseint ((Endtime.gettime ()-nowtime.gettime ())/ +); varDay = parseint (leftsecond/3600/ -); varH =parseint ((leftsecond/3600)% -); Console.log (h); varM =parseint (leftsecond/ -)% -); varS =parseint (leftsecond% -); varhour = day* -+h; $("P"). Text (hour+":"+m+":"+s); if(leftsecond<=0) {TD="xx"; M="xx"; S="xx"; Clearinterval (t); }} fresh (); varT =setinterval (Fresh, +); </script>
First set a function, the last call, after the call set a timer, why to call, because you do not call is a second after the execution, and the function of what should be written, we look at the first, we have to set an end time,. var d = "2016/12/11,20:00:00"; then we put the End function in var endtime = new Date (d);
Then we'll set a current time var nowtime = new Date (), minus the current time with the end time, which is the time of the countdown,
var Leftsecond = parseint ((Endtime.gettime ()-nowtime.gettime ())/1000); from midnight 1970 to the time I set-the current time to get the number of milliseconds, divided by 1000, the last
The result is the number of seconds left.
var day = parseint (LEFTSECOND/3600/24); Get the number of days;
var h =parseint ((leftsecond/3600)%24); Gets the number of hours,
var m =parseint ((LEFTSECOND/60)%60); Gets the number of minutes,
var s =parseint (leftsecond%60); Gets the number of seconds
Here the flexible use of the method of the% take Mo, simply speaking, if you are in addition to the result is the number of days, then the result is the number of hours, if you come out of the result is an hour, then the result is the number of minutes, if you remove the result is a minute, then the result is the number of seconds.
var hour = day*24+h; This is the last hour, the number of days *24 plus the remaining hours, is the total number of hours,
$ ("P"). Text (hour+ ":" +m+ ":" +s), and finally put the resulting result in the P element.
if (leftsecond<=0) {
td= "00";
m= "00";
s= "00";
Clearinterval (t);
}
Let's make a second judgment.
Finally, the function and timing are called.
Fresh ();
var t =setinterval (fresh,1000);
Let's look at the second method.
<script>function Fresh () {varDate =NewDate (); varENDH = at; varENDM = -; varEndS = -; varh =date.gethours (); varm =date.getminutes (); vars =date.getseconds (); varleft = ( at*3600+ -* -+ -)-(h*3600+m* -+s); varHH = Math.floor (left/3600); varMM = Math.floor (left%3600/ -); varSS =left%3600% -; $("P"). Text (hh+":"+mm+":"+SS); if(left<=0) {hh="0"+"0"; MM="0"+"0"; SS="0"+"0"; Clearinterval (t); $("span"). Text ("I love you .")}} fresh ()varT =setinterval (Fresh, +); </script>
The basic idea is to get our final end time, figure out the total number of seconds, minus the total number of seconds in the current time, and finally find the total number of seconds remaining countdown. Finally, the result is obtained by using MO-
The idea is about the same as above.
Let's look at the third method.
<script>varHour= -; varMinute= $; varSecond = -; varShowhour,showminute,showsecond; function Fresh () {varDate =NewDate (); varNowhour =date.gethours (); varNowminute =date.getminutes (); varNowsecond =date.getseconds (); Showhour= hour-Nowhour; if(minute<Nowminute) {Showhour-=1; Showminute=minute+ --Showminute; }Else{Showminute=minute-Nowminute; } if(second<Nowsecond) {Showminute-=1; Showsecond+=second+ --Showminute; }Else{Showsecond=second-Nowsecond; } if(showhour<Ten) {Showhour="0"+Showhour; } if(showminute<Ten) {Showminute="0"+Showminute; } if(showsecond<Ten) {Showsecond="0"+Showsecond; } $("input"). Val (showhour+":"+showminute+":"+Showsecond); if(showhour==0&& showminute==0&& showsecond==0) clearinterval (t); } fresh (); varT =setinterval (Fresh, +); </script>
This way of thinking needs to be noted, we first make a judgment,
if (minute<nowminute) { showhour-=1; Showminute=minute+60-nowminute; } else{ showminute=minute-nowminute; } if (second<nowsecond) {showminute-=1; Showsecond +=second+60-Showsecond;} else{showsecond=second-nowsecond;}
The idea is that when the number of minutes to set is less than the current number of minutes, the number of hours displayed is reduced by 1, and the number of minutes displayed is the number of minutes plus 60-the current number of minutes. If the number of seconds is set less than the current number of seconds, the number of minutes displayed is
Minus 1, and the number of seconds that is displayed is the number of seconds to set-the current number of seconds. That's the basic idea.
The realization of JS Countdown