Skip to content

Instantly share code, notes, and snippets.

@jaredwilli
Created August 19, 2012 13:38
Show Gist options
  • Select an option

  • Save jaredwilli/3394867 to your computer and use it in GitHub Desktop.

Select an option

Save jaredwilli/3394867 to your computer and use it in GitHub Desktop.
Timer - start/stop/update
var timerID = 0,
tStart = null,
clockID = null;
function UpdateTimer() {
if (timerID) {
clearTimeout(timerID);
clockID = 0;
}
if (!tStart) {
tStart = new Date();
}
var tDate = new Date(),
tDiff = tDate.getTime() - tStart.getTime();
tDate.setTime(tDiff);
document.theTimer.theTime.value = tDate.getMinutes() +':'+ tDate.getSeconds();
timerID = setTimeout('UpdateTimer()', 1000);
}
function Start() {
tStart = new Date();
document.theTimer.theTime.value = '00:00';
timerID = setTimeout('UpdateTimer()', 1000);
}
function Stop() {
if (timerID) {
clearTimeout(timerID);
timerID = 0;
}
tStart = null;
}
function Reset() {
tStart = null;
document.theTimer.theTime.value = '00:00';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment