jquery Implementation Countdown (countdown date can be entered on its own) _jquery

Source: Internet
Author: User
Tags getdate

$ (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, which is to deduct the integer dayAfter that, 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 (); })
}) 

Yesterday saw the "Wonderful Taste Class" Countdown video, I learned to write down.

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

DEMO (case) Download

The above is the entire content of this article, I hope to help you, thank you for the support of cloud habitat community!

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.