Write a countdown demo by yourself (similar to group buying websites)

Source: Internet
Author: User

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!

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.