Today, I would like to share with you the countdown JS function, which is similar to the countdown effect of group buying websites because a countdown effect is used in recent projects. I also searched the internet and did not find it better. I wrote it myself ~ It is also very easy to write. For ease of use, I try to encapsulate it as much as possible.
Okay, let's just talk about it.CodeRight...
Function Countdown (end ){ // End is the end time. VaR Starttime = New Date (); VaR Endtime = New Date (date. parse (end. Replace (/-/g ,"/" ))); VaR Dvalue = parseint (endtime. gettime ()-starttime. gettime ()/1000); VaR Strtime = "" ; If (Dvalue> 0 ){ VaR Day = parseint (dvalue/3600/24 ); If (Day> 0 ){ // Consider converting the int type to the string type before determining the length; If (Day. tostring (). length> 1 ) {Strtime + = "<Span>" + day + "</span>" + "day";} Else {Strtime + = "<Span> 0" + day + "</span>" + "day" ;}} Else {Strtime + = "<Span> 00 </span> days" ;} VaR Hour = parseint (dvalue/3600) % 24 ); If (Hour> 0 ){ If (Hour. tostring (). length> 1 ) {Strtime + = "<Span>" + hour + "</span>" + "hour" ;} Else {Strtime + = "<Span> 0" + hour + "</span>" + "hour" ;}} Else {Strtime + = "<Span> 00 </span> hour" ;} VaR Minute = parseint (dvalue/60) % 60 ); If (Minute> 0 ){ If (Minute. tostring (). length> 1 ) {Strtime + = "<Span>" + minute + "</span>" + "points" ;} Else {Strtime + = "<Span> 0" + minute + "</span>" + "points" ;}} Else {Strtime + = "<Span> 00 </span> points" ;} VaR Second = parseint (dvalue % 60 ); If (Second. tostring (). length> 1 ) {Strtime + = "<Span>" + second + "</span>" + "second" ;} Else {Strtime + = "<Span> 0" + second + "</span>" + "second" ;} Return (Strtime );} Else { // Alert ("time has arrived! "); Return "" ;}} // Call Method Function Tick (OBJ, time) {$ ( "#" + OBJ + "" Counter .html (Countdown (time); window. setTimeout ( "Tick ('" + OBJ + "', '" + time + "')", 1000 );}
The above is a major function code, and then you can call it.
The call method is also simple:
<SCRIPT type = "text/JavaScript">
Tick ("element ID", "End Time ");
</SCRIPT>
:
It is too simple, so there is nothing to explain.
Appendix: Demo
PS: Tandy tang I wish you a pleasant time writing your code!