Skip to content

Instantly share code, notes, and snippets.

@pedrocss
Created January 4, 2012 21:57
Show Gist options
  • Save pedrocss/1562393 to your computer and use it in GitHub Desktop.
Save pedrocss/1562393 to your computer and use it in GitHub Desktop.
Timer Javascript
window.onload = function () {
var timeout;
var MIN;
var SEC;
document.getElementById("startTimer").onclick=function(){startTimer()}
function startTimer(){
MIN = document.getElementById("min").value;
SEC = document.getElementById("sec").value;
document.getElementById("startTimer").value = "Stop";
document.getElementById("startTimer").id = "stopTimer";
document.getElementById("stopTimer").onclick=function(){stopTimer()}
refreshTimer(MIN, SEC);
}
function stopTimer(){
document.getElementById("stopTimer").value = "Start";
document.getElementById("stopTimer").id = "startTimer";
document.getElementById("startTimer").onclick=function(){startTimer()}
timeout = clearTimeout(timeout);
}
function refreshTimer(MIN, SEC){
if(SEC == 0){
MIN = MIN - 1;
SEC = 59;
}else{
SEC = SEC - 1;
}
if(MIN+SEC > 0){
document.getElementById("min").value = MIN;
document.getElementById("sec").value = SEC;
timeout = setTimeout(refreshTimer, 1000, MIN, SEC);
}else{
document.getElementById("min").value = MIN;
document.getElementById("sec").value = SEC;
document.getElementById("stop").id = "start";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment