Skip to content

Instantly share code, notes, and snippets.

@64lines
Created April 18, 2014 03:57
Show Gist options
  • Save 64lines/11024221 to your computer and use it in GitHub Desktop.
Save 64lines/11024221 to your computer and use it in GitHub Desktop.
[JQUERY] Conter Progresive and Regresive
/*
* This functions needs JQuery and Three DOM objects
* with id minutes, seconds and hours.
*/
function progresiveCounter() {
var seconds = 0;
var minutes = 0;
var hours = 0;
var strSeconds = "";
var strMinutes = "";
var strHours = "";
setInterval(function() {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
}
if (minutes == 60) {
minutes = 0;
hours++;
}
strSeconds = seconds < 10 ? "0" + seconds : seconds;
strMinutes = minutes < 10 ? "0" + minutes : minutes;
strHours = hours < 10 ? "0" + hours : hours;
$("#seconds").text(strSeconds);
$("#minutes").text(strMinutes);
$("#hours").text(strHours);
}, 1000);
}
function regressiveCounter(hours, minutes, seconds) {
var strSeconds = "";
var strMinutes = "";
var strHours = "";
setInterval(function() {
if (minutes == 0) {
minutes = 60;
hours--;
}
if (seconds == 0) {
seconds = 60;
minutes--;
}
seconds--;
strSeconds = seconds < 10 ? "0" + seconds : seconds;
strMinutes = minutes < 10 ? "0" + minutes : minutes;
strHours = hours < 10 ? "0" + hours : hours;
$("#seconds").text(strSeconds);
$("#minutes").text(strMinutes);
$("#hours").text(strHours);
}, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment