Skip to content

Instantly share code, notes, and snippets.

@marcosbrasil
Created August 29, 2012 20:38
Show Gist options
  • Save marcosbrasil/3518592 to your computer and use it in GitHub Desktop.
Save marcosbrasil/3518592 to your computer and use it in GitHub Desktop.
Regressive time countdown in javascript
var YY = 2012;
var MM = 12;
var DD = 3;
var HH = 14;
var MI = 5;
var SS = 0;
function atualizaContador() {
var hoje = new Date();
var futuro = new Date(YY,MM-1,DD,HH,MI,SS);
var ms = parseInt((futuro - hoje)/1);
var ss = parseInt(ms / 1000);
var mm = parseInt(ss / 60);
var hh = parseInt(mm / 60);
var dd = parseInt(hh / 24);
ms = ms - (ss * 1000);
ss = ss - (mm * 60);
mm = mm - (hh * 60);
hh = hh - (dd * 24);
/*var faltam = '';
faltam += (dd && dd > 1) ? dd+' dias, ' : (dd==1 ? '1 dia, ' : '');
faltam += (toString(hh).length) ? hh+' hr, ' : '';
faltam += (toString(mm).length) ? mm+' min e ' : '';
faltam += ss+' seg';*/
if (dd+hh+mm+ss > 0) {
document.getElementById('contador_dias').innerHTML = (toString(dd).length) ? dd+' dias' : '';
document.getElementById('contador_horas').innerHTML = (toString(hh).length) ? hh+'h' : '';
document.getElementById('contador_minutos').innerHTML = (toString(mm).length) ? mm+'m' : '';
document.getElementById('contador_segundos').innerHTML = ss+'s';
document.getElementById('contador_milisegundos').innerHTML = ms;
setTimeout(atualizaContador,1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment