first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

JavaScript: Timer Code

//(c) 1998 Bryan Wilhite //All rights reserved. //rasx@kintespace.com

//This code assumes that there is a text input object //called "textDisplay" in a FORM block level element. //It is also assumed that there are two buttons. //One calls jsTimerStart() and the other //calls jsTimerStop() when their //onClick() events fire.

var isRunning = false var timeoutID = null var vLater = null

function jsTimer() { vNow = new Date() vNow = vNow.getTime() //Change vDelta into a value in milliseconds. vDelta = vLater - vNow if (vDelta <= 0) { jsTimerStop() } else { //Change vDelta into an object. vDelta = new Date(vDelta) //Change vDelta into an value in seconds. vDelta = vDelta.getSeconds() window.document.forms[1].textDisplay.value = ((vDelta < 10) ? ":0" : ":") + vDelta if (isRunning) { timeoutID = setTimeout("jsTimer()", 1000) } } }

function jsTimerStart(vSeconds) { isRunning = true vNow = new Date() vNow = vNow.getTime() vLater = vNow + (1000 * vSeconds) jsTimer() }

function jsTimerStop() { if (isRunning) { clearTimeout(timeoutID) window.document.forms[1].textDisplay.value = ":00" isRunning = false } else { window.alert("Timer is already stopped.") } }

mod date: 2000-12-21T17:35:19.000Z