jquery implementation of the judgment countdown is ending code _jquery

Source: Internet
Author: User
Tags getdate

This section describes a code example, this code can determine whether the current date has been the countdown to the end, this code does not have the countdown effect, just to determine whether the countdown to complete, such as shopping site concessions, etc., although the actual application, few will appear similar code, but hope to be able to bring some enlightenment to visitors.

The code is as follows:

function done () {
var str=$ (' #end '). Text (); 
var out=str.match (/\d+/g);
Console.log (out); 
var h=parseint (out[0]), M=parseint (Out[1]), S=parseint (out[2)); 
Console.log (h+ ' # ' +m+ ' # ' +s);
var calc=h*3600+m*60+s;
Console.log (calc); 
if (calc==0) {
//code
} 
else{
console.log (' Wait ... ');
} 
var t=settimeout (' Done () ', 1000);
} 

The above is just a snippet of code, can not be demonstrated, the following describes its implementation process.

A. Code comments:

1.function done () {}, this function realizes the judgment countdown end effect.
2.var str=$ (' #end '). Text () to get the textual content in the specified element, the current time should be counted in this code.
3.var Out=str.match (/\d+/g), gets an array of numbers in the time date.
4.var H=parseint (out[0]), M=parseint (Out[1]), S=parseint (out[2)), obtaining hours, minutes, and seconds, respectively.
5.var Calc=h*3600+m*60+s, converted into seconds.
6.if (calc==0) {//code}, determine if the countdown is over, and then specify the appropriate action.
7.var t=settimeout (' Done () ', 1000), performs a judgment function every second.
8.done (), perform this function.

The jquery Implementation Countdown code looks like this:

$ (function () {var tyear = ""; The year entered var tmonth = ""; Enter the month var tdate = ""; Enter the date var iremain = ""; Number of milliseconds between start and end var sdate = ""; Number of days in reverse var shour = ""; Countdown hour var smin = ""; Countdown to the minute var sSec = ""; Countdown to the number of seconds var smsec = ""; 
Milliseconds/Universal tool function, add 0 in single-digit digits, according to the parameters of N, to set the front plus several zero function setdig (num,n) {var str = "" +num; 
while (str.length<n) {str= ' 0 "+str} return str; 
//Get the difference between days, hours, minutes, seconds function getdate () {//Create Start time and end time Date object var ostartdate = new Date (); 
var oenddate = new Date (); 
Gets the value of the text box Tyear = $ ("#tyear"). Val (); 
Tmonth = $ ("#tmonth"). Val (); 
Tdate = $ ("#tdate"). Val (); 
Set End Time Oenddate.setfullyear (parseint (tyear)); 
Oenddate.setmonth (parseint (Tmonth)-1); 
Oenddate.setdate (parseint (tdate)); 
Oenddate.sethours (0); 
Oenddate.setminutes (0); 
Oenddate.setseconds (0); 
Find the number of seconds (divided by 1000) of the start and end times Iremain = (Oenddate.gettime ()-ostartdate.gettime ())/1000; 
The total number of seconds divided by the number of seconds in a day, and then take out the integer part, we can see how many days. 
Sdate = Setdig (parseint (iremain/(60*60*24)), 3); The total number of seconds divided by the number of seconds in a day, and then the remainder, that is, after the integer day is deducted, the total number of seconds remaining. 
Iremain%= 60*60*24; 
The total number of seconds remaining divided by the number of seconds in one hours, and then the integer part, is how many hours. 
Shour = Setdig (parseint (iremain/(60*60)), 2)//The remaining total number of seconds divided by one hours of seconds, and then take the remainder, this remainder, is deducted hours after this, the remaining total number of seconds. 
Iremain%= 60*60; 
The total number of seconds remaining divided by the number of seconds in a minute, and then the integer part, that is, how many minutes. 
Smin = Setdig (parseint (IREMAIN/60), 2)//The total number of seconds left divided by the number of seconds in a minute, and then the remainder, which is the total number of seconds left after deducting minutes. 
iremain%=60; 
The remaining number of seconds SSec = Setdig (iremain,2); 
Number of milliseconds smsec = ssec*100; 
//Change the display time function Updateshow () {$ (". Showdate span"). Text (tyear+ "-" +tmonth+ "-" +tdate); 
$ (". Count span"). each (function (index, Element) {if (index==0) {$ (this). Text (sdate); 
}else if (index==1) {$ (this). Text (Shour); 
}else if (index = = 2) {$ (this). Text (smin); 
}else if (index = = 3) {$ (this). Text (SSEC); 
}else if (index = = 4) {$ (this). Text (SMSEC); 
} 
}); 
//Every second to perform a time update function Autotime () {getdate (); 
If less than 0, the purge calls itself and returns if (iremain<0) {cleartimeout (sett); 
Return 
} updateshow (); 
var sett = settimeout (autotime,1000); 
//Click the button to start timing $ ("button"). Click (function () {autotime (); }) 
})

Record where you need to be aware:

1. Modulo operation:

Iremain%= 60*60*24;

is to return the remainder, and the remainder in this instance is the number of seconds left after the integer is taken away.

2. Tool function Setdig (num,n)

You can automatically add 0 to the incoming number based on the parameters passed in

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.