»
EnglishFrenchVietnamese

Print - A Stopwatch script - JavaScriptBank.com

Full version: jsB@nk » Time » Counter » A Stopwatch script
URL: https://www.javascriptbank.com/a-stopwatch-script.html

A Stopwatch script © JavaScriptBank.comIt's always fun to play with a stopwatch - even in JavaScript! This stopwatch does even include the basic features of Start, Stop, and of course, Reset. By the way, the time is not the regular seconds readout that you are probably used to. It displays in milliseconds! Therefore, 1000 = 1 sec, 5000 = 5 sec, etc...

Full version: jsB@nk » Time » Counter » A Stopwatch script
URL: https://www.javascriptbank.com/a-stopwatch-script.html



JavaScript
<SCRIPT language=JavaScript><!--// set initial valuesvar timerRunning = falsevar timerID = null// create instance of Date object representing current timevar initial = new Date()// start timerfunction st() {// ask the user if to reset the timer;if (confirm("Would you like to reset the timer?"))// set global variable to new time{initial = new Date();}else {return}// set the button's label to "stop"document.forms[0].general.value = "stop";// assign the stop function reference to the button's onClick event handlerdocument.forms[0].general.onclick = stop;// assign milliseconds since 1970 to global variablestartTime = initial.getTime();// make sure the timer is stoppedstopTimer();// run and display timershowTimer();}// set button to initial settingsfunction stop() {// set the button's label to "start"document.forms[0].general.value = "start";// assign the start function reference to the button's onClick event handlerdocument.forms[0].general.onclick = st;// stop timerstopTimer();}// stop timerfunction stopTimer() {// if the timer is currently runningif (timerRunning){// clear the current timeout (stop the timer)clearTimeout(timerID);}// assign false to global variable because timer is not runningtimerRunning = false}function showTimer() {// create instance of Date representing current timervar current = new Date();// assign milliseconds since 1970 to local variablevar curTime = current.getTime();// assign difference in milliseconds since timer was clearedvar dif = curTime - startTime;// assign difference in seconds to local variablevar result = dif / 1000;// is result is not positiveif (result < 1){// attach an initial "0" to beginningresult = "0" + result;}// convert result to stringresult = result.toString();// if result is integerif (result.indexOf(".") == -1){// attach ".00" to endresult += ".00";}// is result contains only one digit after decimal pointif (result.length - result.indexOf(".") <= 2){// add a second digit after pointresult += "0";}// place result in text fielddocument.forms[0].display.value = result;// call function recursively immediately (must use setTimeout to avoid overflow)timerID = setTimeout("showTimer()", 0);// timer is currently runningtimerRunning = true;}// --></SCRIPT><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<FORM><INPUT onfocus=this.blur() name=display size="20"> <INPUT onclick=st() type=button value=start name=general> </FORM><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->