The most comprehensive JS countdown code _javascript Tips

Source: Internet
Author: User

The First: JavaScript countdown code for accurate to second

HTML code:

<form name= "Form1" > <div align= "center" align= "Center" > <center> from 2010 and:<br> <input type= "TextArea" name= "left" size= "style=" Text-align:center "> </center> </div> </form> <script 
language= "JavaScript" > Startclock () var timerid = null; 
var timerrunning = false; 
function Showtime () {today = new Date (); 
var nowhour = today.gethours (); 
var nowminute = Today.getminutes (); 
var nowmonth = Today.getmonth (); 
var nowdate = Today.getdate (); 
var nowyear = Today.getyear (); 
var nowsecond = Today.getseconds (); 
if (nowyear <2000) nowyear=1900+nowyear; 
today = NULL; Hourleft = 23-nowhour Minuteleft = 59-nowminute Secondleft = 59-nowsecond Yearleft = 2009-nowyear Monthleft = 
12-nowmonth-1 Dateleft = 31-nowdate if (secondleft<0) {secondleft=60+secondleft; 
minuteleft=minuteleft-1; 
} if (minuteleft<0) {minuteleft=60+minuteleft; 
hourleft=hourleft-1; } if (hourleft<0) {hourleft=24+hourleft; 
dateleft=dateleft-1; 
} if (dateleft<0) {dateleft=31+dateleft; 
monthleft=monthleft-1; 
} if (monthleft<0) {monthleft=12+monthleft; 
yearleft=yearleft-1; 
} temp=yearleft+ ' year, ' +monthleft+ ' month, ' +dateleft+ ' Day, ' +hourleft+ ' hour, ' +minuteleft+ ' minutes, ' +secondleft+ ' seconds ' 
Document.form1.left.value=temp; 
Timerid = settimeout ("Showtime ()", 1000); 
Timerrunning = true; 
var timerid = null; 
var timerrunning = false; 
function Stopclock () {if (timerrunning) cleartimeout (Timerid); 
Timerrunning = false; 
function Startclock () {stopclock (); 
Showtime (); 

 }//--> </script>

The second: So-and- so games

HTML code:

<!--countdown JavaScript begin--> <script language= "JavaScript" > <!--function DigitalTime1 () {var deadline = new Date ("08/13/2007")//Opening countdown var symbol= "August 13" var now = new Date () var diff = -480-now.gettimezoneoffset ()//is North Time difference between Beijing and local time var leave = (Deadline.gettime ()-now.gettime ()) + diff*60000 var day = Math.floor (Leave/(1000 * 60 * 60 *)) var hour = Math.floor (Leave/(1000*3600))-(day *) var minute = Math.floor (Leave/(1000*60))-(Day * 24 *6 0)-(hour *) var second = Math.floor (Leave/(1000))-(Day * *60*60)-(hour)-(minute*60) var Deadl ine_2= new Date ("08/13/2004")///opening timer var symbol_2= "August 13" var now_2 = new Date () var diff_2 = -480-now.gettimezoneof Fset ()//is the time difference between Beijing and local time var leave_2 = (Now_2.gettime ()-deadline_2.gettime ()) + diff_2*60000 var day_2 = Math.floor (Lea Ve_2/(1000 *)) var hour_2 = Math.floor (leave_2/(1000*3600))-(day_2) var minute_2 = Math.floor (le Ave_2/(1000*60))-(DAY_2 *60)-(hour_2 *) var second_2 = Math.floor (leave_2/(1000))-(day_2 * *60*60)-(hour_2)-(m 
INUTE_2*60) day=day+1; 

day_2=day_2+1; if (day>0)//has not yet opened {//liveclock1.innerhtml = "Now" +symbol+ "Day" liveclock1.innerhtml = "<font settimeout (" DigitalTi Me1 () ", 1000)} if (day<0)//has opened {//liveclock1.innerhtml =" +symbol+ "and" +day+ "Days" +hour+ "" Hours "+minute+" Minutes "+second + "seconds" liveclock1.innerhtml = "<font settimeout" ("DigitalTime1 ()", 1000)} if (day==0)//opening {//liveclock1.innerh TML = "Now" +symbol+ "days" liveclock1.innerhtml = "<font settimeout (" DigitalTime1 () ", 1000)} if (Day<0 & Day 
_2&GT;19)//So-and-so games end {//liveclock1.innerhtml = "+symbol+" and "+day+" +hour+ "hours" +minute+ "minutes" +second + "seconds" liveclock1.innerhtml = "<font settimeout (" DigitalTime1 () ", 1000)}}//--> </script> <!--countdown JAVASCR

 IPT end--> <body onload=digitaltime1 () > <div id= liveclock1></div> </body>

The third type: hour countdown

HTML code:

<script language= "JavaScript" > 
<!-- 
var maxtime = 60*60//One hours, per second, adjust yourself! 
function countdown () { 
if (maxtime>=0) { 
minutes = Math.floor (MAXTIME/60); 
seconds = Math.floor (maxtime%60); 
msg = "Distance end also has" +minutes+ "+seconds+" "Seconds"; 
document.all["Timer"].INNERHTML=MSG; 
if (MaxTime = = 5*60) alert (' Note, there are 5 minutes! '); 
--maxtime; 
} 
else{ 
clearinterval (timer); 
Alert ("Time to, end!"); 
} 
Timer = SetInterval ("Countdown ()", 1000); 
--> 
</SCRIPT> 
<div id= "Timer" style= "color:red" ></div> 

The Fourth kind: the simplest countdown

HTML code:

<script language= "JavaScript" > 
<!--Begin 
var timedate= new Date ("January 14,2006"); 
var times= "Postgraduate examination"; 
var now = new Date (); 
var date = Timedate.gettime ()-now.gettime (); 
var time = Math.floor (Date/(1000 *)); 
if (time >= 0); 
document.write ("<li><font color= #DEDBDE > Now from 2006" +times+ "also: <font color= #ffffff ><b>" + Time + "</b></font> day </font></li>"); 
End--> 
</Script> 

The fifth kind: the simplest countdown second

HTML code:

<script language= "JavaScript" type= "text/javascript" > 
function djs () { 
var urodz= new Date ("11/12/2008") ); 
var now = new Date (); 
var num 
var ile = Urodz.gettime ()-now.gettime ();   
var dni = Math.floor (ile/(1000 * 60 * 60 * 24)); 
if (dni >1) 
num=dni+1 
else if (dni = = 1) num=2 
else if (dni = 0) num=1 
else 
num=0 Ment.write (num) 
} 
</script> 

From the opening ceremony also [<script language= "JavaScript" type= "Text/javascript" >djs () </script>]

The sixth kind: JavaScript countdown Timer-use system time self-verification
The use of system time self-test countdown, without manual adjustment so that the countdown is more accurate, code and detailed comments as follows:

<span id= "Clock" >00:01:11:00</span> <input id= "STARTB" type= "button" value= "Start countdown!" onclick= "Run ()" > <input id= "endb" type= "button" value= "Stop countdown!" onclick= "Stop ()" > <br> <input id= "dif" F "type=" text > <input id= "Next" type= "text" > <script language= "Javascript" >/* notice must be unto

Uched at the all times. Countdown.js v. 1.0 The latest version is available at http://blog.csdn.net/yjgx007 Copyright (c) xinyi.chen. 
All rights reserved. 
Created 7/30/2004 by Xinyi.chen. web:http://blog.csdn.net/yjgx007 e-mail:chenxinyi1978@hotmail.com last modified:7/30/2004-free softw 
are Can redistribute it and/or modify it under the terms of the GNU general public License as published by the free SOFTW

are Foundation; 
License at http://www.gnu.org/copyleft/gpl.html for more details. 
* * var normalelapse = 100; 
var nextelapse = Normalelapse; 
var counter; var sTarttime; 
var start = Clock.innertext; 
var finish = "00:00:00:00"; 

var timer = null; 
Start function Run () {startb.disabled = true; 
endb.disabled = false; 
Counter = 0; 

Initialization start time StartTime = new Date (). valueof (); Nextelapse is a timed time, initially for 100 milliseconds//Note setinterval function: After the time passed Nextelapse (milliseconds), OnTimer began to execute timer = window.setinterval ("OnTimer 
() ", nextelapse); 
//stop running function stop () {startb.disabled = false; 
Endb.disabled = true; 
Window.cleartimeout (timer); 
} window.onload = function () {endb.disabled = true; 
}//Countdown function OnTimer () {if (start = finish) {window.clearinterval (timer); 
Alert ("Time is up!"); 
Return 
var HMS = new String (start). Split (":"); 
var ms = new Number (hms[3]); 
var s = new number (hms[2]); 
var m = new number (hms[1]); 
  
var h = new number (hms[0]); 
MS-= 10; 
if (MS < 0) {ms = 90; 
S-= 1; 
 if (s < 0) {s = 59; 
M-= 1; 
 } if (M < 0) {m = 59; 
H-= 1; } var ms = Ms < 10? ("0" + ms): MS; var ss = S < 10? 
("0" + s): s; var sm = m < 10? 
("0" + m): M; var sh = h < 10? 

("0" + H): h; 
Start = sh + ":" + SM + ":" + SS + ":" + ms; 

Clock.innertext = start; 

Clear the last timer window.clearinterval (timer); 
The time difference of the self-checking system is obtained, and the time Nextelapse counter++ of the new timer which is started next is obtained. 
var countersecs = counter * 100; 
var elapsesecs = new Date (). valueof ()-starttime; 
var diffsecs = countersecs-elapsesecs; 
Nextelapse = Normalelapse + diffsecs; 
Diff.value = Countersecs + "-" + elapsesecs + "=" + diffsecs; 
Next.value = "Nextelapse =" + nextelapse; 

if (Nextelapse < 0) Nextelapse = 0; 
Start the new timer timer = Window.setinterval ("OnTimer ()", nextelapse);

 } </script>

Attach a full countdown and time replication to use the results of the operation are:

Day hours, January 22, 2013 15:44:23 Tuesday

 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.