Countdown with minutes and seconds with Jquery

Asked

Viewed 1,009 times

0

How do I get a countdown with jquery but only in minutes and second -> 04:00 03:59 03:58... with option to stop, restart and increase and decrease the time someone could give me an example?

code:

    var tempo = $("#tempo").text("05:00");
tempo = new Number();
// Tempo em segundos
tempo = 300;
function startCountdown() {
// Se o tempo não for zerado
    if ((tempo - 0) >= 0) {
        // Pega a parte inteira dos minutos
        var min = parseInt(tempo / 60);
        // Calcula os segundos restantes
        var seg = tempo % 60;
        // Formata o número menor que dez, ex: 08, 07, ...
        if (min < 10) {
            min = "0" + min;
            min = min.substr(0, 2);
        }
        if (seg <= 9) {
            seg = "0" + seg;
        }

        // Cria a variável para formatar no estilo hora/cronômetro
        horaImprimivel = min + ':' + seg;


        //JQuery pra setar o valor
        $("#tempo").html(horaImprimivel);

        // Define que a função será executada novamente em 1000ms = 1 segundo
        setTimeout('startCountdown()', 1000);

        // diminui o tempo
        tempo--;


    } else {

    }

}

the code counts backwards but how do I pause and restart?

  • Do you know Javascript? What have you tried to do?

  • I tried to use Countdown with jquery but n I was able to use without date by the plugin documentation since there are only examples to count leaving for a specific date.

  • It’s something that’s not hard to do even with pure JS

  • Again the question: do you know Javascript? Do not try to use jQuery without knowing Javascript.

  • I know a little bit of Javascript, I know a little bit of jquery

  • Start with Javascript as Anderson indicated. You’ll need to use at least setInterval, clearInterval. Then manipulate the minutes and seconds manually

  • An aid: https://jsfiddle.net/44d1h9xw/

  • edited my question with my countdown code that is already working need now pause and restart.

Show 3 more comments
No answers

Browser other questions tagged

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