Jquery determines whether the countdown ends the code. jquery countdown

Source: Internet
Author: User
Tags time and date

Jquery determines whether the countdown ends the code. jquery countdown

This section describes a code example. This code can determine whether the countdown has ended on the current date. This Code does not have a countdown effect, but just determines whether the countdown is complete, such as the Discount Period of a shopping website, although similar code is rarely used in practical applications, it is hoped that it will bring some inspiration to viewers.

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);} done ();

The above is only a code snippet and cannot be demonstrated. The following describes the implementation process.

I. Code comments:

1. function done () {}, this function is used to determine the end Of the countdown.
2. var str = $ ('# end'). text () to get the text content in the specified element. The current time should be counted down in this Code.
3. var out = str. match (/\ d +/g) to obtain the array of numbers in the time and date.
4. var h = parseInt (out [0]), m = parseInt (out [1]), s = parseInt (out [2]), get the hour, minute, and second respectively.
5. var calc = h * 3600 + m * 60 + s, converted to seconds.
6. if (calc = 0) {// code}, determine whether the countdown is over, and then specify the corresponding operation.
7. var t = setTimeout ('done () ', 1000). The judgment function is executed every second.
8. done (), run this function.

The jquery countdown code is as follows:

$ (Function () {var tYear = ""; // enter the year var tMonth = ""; // enter the month var tDate = ""; // input date var iRemain = ""; // The number of milliseconds between start and end var sDate = ""; // the number of days of the Countdown var sHour = ""; // countdown hours var sMin = ""; // countdown minutes var sSec = ""; // countdown seconds var sMsec = ""; // millisecond count // common tool function. Add zero to the single digit. Based on the passed N parameter, add the preceding Zero function setDig (num, n) {var str = "" + num; while (str. length <n) {str = "0" + str} return str;} // returns the day, hour, minute, and second function getdate () of the difference () {// Create a Date object named var oStartDate = new Date (); var oEndDate = new Date (); // obtain the text box value tYear =$ ("# tyear "). val (); tMonth = $ ("# tmonth "). val (); tDate = $ ("# tdate "). val (); // set the end time oEndDate. setFullYear (parseInt (tYear); oEndDate. setMonth (parseInt (tMonth)-1); oEndDate. setDate (parseInt (tDate); oEndDate. setHours (0); oEndDate. setMinutes (0); oEndDate. setSeconds (0); // obtain the number of seconds for the start and end time (divided by 1000) iRemain = (oEnd Date. getTime ()-oStartDate. getTime ()/1000; // The total number of seconds divided by the number of seconds in a day. Then, the integer is taken out to determine the number of days. SDate = setDig (parseInt (iRemain/(60*60*24), 3); // The total number of seconds divided by the number of seconds of the day, and then the remainder of the value is obtained, the total number of seconds after the integer day is deducted. IRemain % = 60*60*24; // The total number of seconds remaining is divided by the number of seconds in one hour, and then the integer is the number of hours. SHour = setDig (parseInt (iRemain/(60*60), 2) // The total number of remaining seconds divided by the number of seconds in one hour, and then the remaining number, the remainder, the total number of seconds after the hour is deducted. IRemain % = 60*60; // The total number of seconds remaining divided by the number of seconds in one minute, and then the integer is the number of minutes. SMin = setDig (parseInt (iRemain/60), 2) // divide the remaining total number of seconds by the number of seconds in one minute, and then take the remaining number. The remainder is after the minute is deducted, the total number of seconds remaining. IRemain % = 60; // The remaining seconds sSec = setDig (iRemain, 2); // The 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) ;}}) ;}// execute a time update function autoTime () {getdate () every second; // if it is less than zero, clear the caller, and return if (iRemain <0) {clearTimeout (setT); return;} updateShow (); var setT = setTimeout (autoTime, 1000 );} // click the button to start timing $ ("button "). click (function () {autoTime ();})})

Notes:

1. modulo operation:

IRemain % = 60*60*24;

Returns the remainder. The remainder in this instance is the number of seconds after the integer is taken away.

2. tool function setDig (num, n)

You can automatically add zero before the input number based on the input parameters.

Articles you may be interested in:
  • JQuery-based countdown plug-in code
  • Sort out eight great jQuery countdown plug-ins and tutorials
  • Implementation Code for JQuery to implement the countdown button
  • JQuery-based countdown implementation code
  • How to Implement the second kill in jquery countdown Effect
  • How to Implement the countdown button using JQuery
  • Countdown to implemented buttons in the success callback function of jquery ajax
  • Jquery countdown code for sending verification codes on mobile phones
  • Example of Countdown jump implemented by jQuery

Related Article

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.