JavaScript to achieve simple digital countdown _javascript Tips

Source: Internet
Author: User
Tags diff getdate local time setinterval

Here is a way to show the countdown in JavaScript, from 101 seconds down to the end of the timer (0), the code is as follows:

Runcount (ten);
function Runcount (t) {
  if (t>0) {
    document.getElementById (' Shownum '). InnerHTML = t;
    T-;
    settimeout (function () {runcount (t);},1000);
  } else{
    document.getElementById (' Shownum '). InnerHTML = ' Countdown's over! ';
  }
}

Before running the JS code, you need to add a div with ID shownum.

JS implementation of dynamic Countdown function * A is the time stamp received from the background, you need to convert to milliseconds unit

<div> from next year: <span id= ' Daya ' ></span> days <span id= ' Hoursa ' ></span> hour <span id= ' Minu A ' ></span> min <span id= ' Secoa ' ></span> sec </div> <script type= "Text/javascript" >/** * * C     Opyright WE 2012.7 * JS to implement dynamic Countdown function * A is received from the background of the timestamp, to be converted to milliseconds unit * * * * * * var a=12345678;
    In milliseconds, the function fomtime () {a=a-1000;
    var b=new Date ();
    B.settime (0);
    var c=new Date ();
    C.settime (a);    var day1=b.getdate ();
    In order to facilitate the call, the number of days, hours and so on separate definition of Var hours1=b.gethours ();
    var minu1=b.getminutes ();
    var seco1=b.getseconds ();
    var day2=c.getdate ();
    var hours2=c.gethours ();
    var minu2=c.getminutes ();
    var seco2=c.getseconds ();
    var day=day2-day1;
    var hours=hours2-hours1;
    var minu=minu2-minu1;
    var Seco=seco2-seco1;
    document.getElementById (' Daya '). Innerhtml=day;
    document.getElementById (' Hoursa '). innerhtml=hours;
    document.getElementById (' Minua '). Innerhtml=minu; Document.getelementbyiD (' Secoa '). Innerhtml=seco;
  SetTimeout ("Fomtime ()", 1000);
} fomtime ();

 </script>

Countdown by day

HTML code 1:

<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> 

HTML Code 2:

<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 *)); 
 if (DNI >1) num=dni+1
 else if (dni = = 1) num=2 
 else if (dni = 0) num=1
 else num=0 
 document.write (num) c12/>} 
</script> 

From the opening ceremony also [<script language= "JavaScript" type= "Text/javascript" >djs () </script>]
JavaScript countdown code for accurate to second

HTML code:

<form name= "Form1" > <div align= "center" align= "Center" > <center> from 2013 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 Mont 
   Hleft = 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+ ', ' +secondleft+ ' seconds ' document.form1.l 
  Eft.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>

Certain games, different stages according to the time

HTML code:

<!--countdown JavaScript begin--> <script language= "JavaScript" > <!--function DigitalTime1 () {var deadl  ine= new Date ("08/13/2007")//Opening countdown var symbol= "August 13" var now = new Date () var diff = -480-now.gettimezoneoffset ()  Time difference between Beijing and local time var leave = (Deadline.gettime ()-now.gettime ()) + diff*60000 var day = Math.floor (Leave/(1000 * 60  * (*)) var hour = Math.floor (Leave/(1000*3600))-(day *) var minute = Math.floor (Leave/(1000*60))-(Day 

 * *60)-(hour *) var second = Math.floor (Leave/(1000))-(Day * *60*60)-(hour)-(minute*60) var deadline_2= new Date ("08/13/2004")///opening timer var symbol_2= "August 13" var now_2 = new Date () var diff_2 = -480-now.g Ettimezoneoffset ()//is the time difference between Beijing and local time var leave_2 = (Now_2.gettime ()-deadline_2.gettime ()) + diff_2*60000 var day_2 = M Ath.floor (leave_2/1000)) var hour_2 = Math.floor (leave_2/(1000*3600))-(day_2 *) var minute_2 = Math.floor (Leave_2/(1000*60)-(day_2 *60)-(hour_2 *) var second_2 = Math.floor (leave_2/(1000))-(day_2 * *60*60)-(hour_2 
 *)-(minute_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 (" Dig ItalTime1 () ", 1000)} if (day<0)//has opened {//liveclock1.innerhtml =" now from "+symbol+" and "+day+" Day +hour+ "hours" +minute+ "Minute" +second + "sec" liveclock1.innerhtml = "<font settimeout (" DigitalTime1 () ", 1000)} if (day==0)//Opening /liveclock1.innerhtml = "Now" +symbol+ "Day" liveclock1.innerhtml = "<font settimeout (" DigitalTime1 () ", 1000)} if (Day<0 & day_2>19)//Certain games end {//liveclock1.innerhtml = "now away from" +symbol+ "and" +day+ "Day" +hour+ "hours" +minute+ "points" +seco nd + "sec" liveclock1.innerhtml = "<font settimeout (" DigitalTime1 () ", 1000)}}//--> </script> <! --Countdown JavaScript end--> <body onload=digitaltime1 () > <div id= liveclock1></div> </body>

 

Countdown by Hour

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> 

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" > 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 ("OnTime  
R () ", 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>

The above is the entire contents of this article, I hope you can enjoy.

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.