Timer with minute hour day and second

Asked

Viewed 3,916 times

1

1 answer

2


I think something like that should work for what you need:

var data = '2016/02/28';
var falta = (new Date(data).getTime() - new Date().getTime()) / 1000;
var segundos = Math.round(falta % 60);
var minutos = Math.round(falta / 60 % 60);
var horas = Math.round(falta / 60 / 60 % 24);
var dias = Math.round(falta / 60 / 60 / 24);
var divs = document.querySelectorAll('div');

setInterval(function () {
    if (segundos == 0) {
        segundos = 60;
        minutos--;
    }
    if (minutos == 0) {
        minutos = 60;
        horas--;
    }
    if (horas == 0) {
        horas = 24;
        dias--;
    }
    segundos--;
    var contador = [dias, horas, minutos, segundos].forEach(function (parcela, i) {
        divs[i].innerHTML = parcela;
    });

}, 1000);

jsFiddle: http://jsfiddle.net/ukndtuzu/

Alternatively you can use the plugin that is used on the page you mentioned: http://hilios.github.io/jQuery.countdown/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.