Some shopping websites often encounter countdown functions. For example, some products are engaged in activities within a certain period of time. The following small series will share with you a piece of code about jquery implementation to determine whether the countdown ends, I hope to help you. This chapter introduces a code example. This code can determine whether the countdown has ended on the current date. This Code does not show the countdown effect, but just determines whether the countdown is complete, for example, the promotion period of a shopping website. Although similar code is rarely used in actual 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
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.