Skip to content

Instantly share code, notes, and snippets.

@nadeemelahi
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadeemelahi/10367539 to your computer and use it in GitHub Desktop.
Save nadeemelahi/10367539 to your computer and use it in GitHub Desktop.
count down timer
<p id="whiteTimeLeft"></p>
<button id="start">start</button>
<button id="stop">stop</button>
var secs = 599;
function decrementTimer() {
var currentMinutes = "0" + Math.floor(secs / 60);
var currentSeconds = Math.floor((secs % 60));
if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
var timeLeft = currentMinutes + ":" + currentSeconds;
$("#whiteTimeLeft").text(timeLeft);
secs--
}
var clearDecrementTimer;
$("#start").click(function(){
clearDecrementTimer = setInterval(function(){
decrementTimer()
}, 1000);
});
$("#stop").click(function(){
clearInterval(clearDecrementTimer);
});
===============================================================================================================
<p id="whiteTimeLeft"></p>
var decrementTimer = function(secs) {
var currentMinutes = "0" + Math.floor(secs / 6000);
var currentSeconds = Math.floor((secs%60));
if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
var timeLeft = currentMinutes + ":" + currentSeconds;
console.log(timeLeft);
$("#whiteTimeLeft").text(timeLeft);
if(secs > 0) setTimeout(function(){decrementTimer(secs-1)},1000);
};
decrementTimer(59999);
===============================================================================================================
<p id="whiteTimeLeft"></p>
var element = document.getElementById("whiteTimeLeft");
var time = new Date();
time.setMinutes(10);
time.setSeconds(0);
function decrementTimer() {
element.innerHTML = time.getMinutes() + ":" + time.getSeconds(); // todo: pad zeroes
time.setSeconds(time.getSeconds() - 1);
setTimeout(decrementTimer, 1000);
}
decrementTimer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment