JavaScript Learning Notes (iv) Countdown program code _ Basics

Source: Internet
Author: User
First look at 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> <script type=" text/ JavaScript "> Function countdown (enddate) {var now = new Date (); var deadtime = document.getElementById (EndDate); var deadline = new Date (deadtime.value); The minute difference between local time and Greenwich Mean Time (GMT) is var timediff = Now.gettimezoneoffset (); There is some confusion here, if the minute difference into milliseconds should be timediff*60*1000, but this return of the data is not correct! 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 kinds of computational 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 = "+deadtime.value+" already has "+countday+" Day "+counthour+" hour "+countminute+" minute "+countsecond+" seconds "; else {outstr = "distance" +deadtime.value+ "+countday+" "Days" +counthour+ "hours" +countminute+ "minutes" +countsecond+ "seconds"; var showTime = document.getElementById ("ShowTime"); showtime.innerhtml = outstr; } </script> </pead> <body> <p> <label for= "Deadline" > Due 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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

HTML Section Code:
You can enter the countdown time in the text box, and if the current time is less than the cutoff time, return normally, otherwise, return the distance to the present time
Copy Code code as follows:

<p>
<label for= "Deadline" > Due 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>

JavaScript Part code:
Copy Code code as follows:

function Countdown (EndDate) {
var now = new Date ();
var deadtime = document.getElementById (EndDate);
var deadline = new Date (deadtime.value);
Minute difference between local time and Greenwich Mean Time (GMT)
var Timediff = Now.gettimezoneoffset ();
There is some confusion here, if the minute difference into milliseconds should be timediff*60*1000, but this return of the data is not correct!
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 kinds of 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+ "+countday+" "Days" +counthour+ "hours" +countminute+ "minutes" +countsecond+ "seconds";
} else {
outstr = "Distance" +deadtime.value+ "still Poor" +countday+ "Days" +counthour+ "hours" +countminute+ "minutes" +countsecond+ "seconds";
}
var showTime = document.getElementById ("ShowTime");
showtime.innerhtml = outstr;
}

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.