<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= "diff" type= "text" <input id= "Next" type= "text" <script language= "Javascript" var normalelapse = $; var nextelapse = Normalelapse; var counter; var starttime; var start = Clock.innertext; var finish = "00:00:00:00"; var timer = null; Start running function Run () { Startb.disabled = true; endb.disabled = false; Counter = 0; Initialization start time StartTime = new Date (). valueof (); Nextelapse is timed to be 100 milliseconds at initial time Note the SetInterval function: After the time elapses nextelapse (milliseconds), OnTimer begins execution 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 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) { & nbsp; 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 of the next new timer is Nextelapse counter++; 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 a new timer timer = Window.setinterval ("OnTimer ()", Nextelapse); } </script> |