»
Tiếng AnhTiếng PhápTiếng Việt

In - Đồng hồ bấm giờ - JavaScriptBank.com

Phiên bản đầy đủ: jsB@nk » Thời gian » Bộ đếm » Đồng hồ bấm giờ
URL: https://www.javascriptbank.com/a-stopwatch-script.html

Đồng hồ bấm giờ © JavaScriptBank.comHiệu ứng tạo một đồng hồ bấm giờ với thời gian tăng dần khi bạn nhấn vào nút Start.

Phiên bản đầy đủ: jsB@nk » Thời gian » Bộ đếm » Đồng hồ bấm giờ
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-->