Javascript learning notes (4) countdown program code

Source: Internet
Author: User

First, check all the complete code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta content = "text/ html; charset = UTF-8 "http-equiv =" Content-Type "/> <title> countDown test </title> </pead> <body> <p> <label for =" deadline "> end date </label> <input type =" text "value =" 2011-06-11 "id =" deadline "/> <input type =" button "value =" Start countdown" onclick = "window. setInterval ('countdown (\ 'deadline \ ')', 1000) "/> </p> <p id =" showTime "> </p> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Some html code:
You can enter the countdown time in the text box. If the current time is less than the deadline, the system returns normally. Otherwise, the system returns the time from the deadline to the present.
Copy codeThe Code is as follows:
<P>
<Label for = "deadline"> deadline </label>
<Input type = "text" value = "2011-06-11" id = "deadline"/>
<Input type = "button" value = "Start countDown" onclick = "window. setInterval ('countdown (\ 'adline \ ')', 1000)"/>
</P>
<P id = "showTime"> </p>

Javascript code:
Copy codeThe Code is as follows:
Function countDown (endDate ){
Var now = new Date ();
Var deadtime = document. getElementById (endDate );
Var deadline = new Date (deadtime. value );
// The minute difference between the local time and Greenwich Mean Time (GMT)
Var timeDiff = now. getTimezoneOffset ();
// There is some confusion here. If the minute difference is converted into milliseconds, it should be timeDiff * 60*1000, but the returned data is incorrect!
Var leave = Math. abs (deadline. getTime ()-now. getTime () + timeDiff * 60 );
Var minute = 1000*60;
Var hour = minute * 60;
Var day = hour * 24;
Var countDay = Math. floor (leave/day );
// Var countHour = Math. floor (leave-day * countDay)/hour); two calculation ideas
Var countHour = Math. floor (leave/hour-countDay * 24 );
Var countMinute = Math. floor (leave/minute)-countDay * 24*60-countHour * 60;
Var countSecond = Math. floor (leave/1000)-countDay * 24*60*60-countHour * 60*60-countMinute * 60;
Var outStr = "";
If (deadline <now ){
OutStr = "distance" + deadtime. value + "existing" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
} Else {
OutStr = "distance" + deadtime. value + "poor" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
}
Var showTime = document. getElementById ("showTime ");
ShowTime. innerHTML = outStr;
}

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.