Create a javascript timer

Asked

Viewed 1,328 times

-1

I wonder if anyone can help me create a timer, a counter in the existing code. It’s like 600, but I’d like to create the time that would be like 10:00, being minutes and seconds.

<html lang="br">
    <head>
        <title>Contador com barra de progresso</title>
        <meta charset="utf-8">
        <style>
            .barra-progresso{width: 406px;height: 24px;background-color: #bbb;border-radius: 13px;padding: 3px;margin: 50px auto;}
            .porcentagem{width: 100%;height: 24px;border-radius: 10px;background-color: dodgerblue;}
        </style>
        <script>
            var g_iCount = new Number();
            var g_iCount = 600+1;

            function contagem(){
                var prg = document.getElementById('progresso-da-barra');
                   if((g_iCount - 1) >= 0){

                            g_iCount = g_iCount - 1;

                            mostracontagem.innerText =  g_iCount;

                            tempo_restante = g_iCount / 6;
                            prg.style.width = tempo_restante + '%';
                            setTimeout('contagem()',1000);
                   }
            }
        </script>
    </head>
    <body onload="contagem();">
        <div class="barra-progresso">
            <div class="porcentagem" id="progresso-da-barra"></div>
            <div id="mostracontagem"><div>
        </div>
    </body>
</html>     
  • the question was unclear, 10:00 like? in minutes and hours?

  • minutes and seconds.

1 answer

-1

Try to follow this template and adapt to your code:

function contagem() {
  timer = 600;
  return function() {
    var minutos,
        segundos;

    minutos = Math.floor(timer/60);
    segundos = timer%60 <= 9 ? '0' + timer%60 : timer%60;
    document.getElementById('mostracontagem').innerHTML = minutos + ':' + segundos;
    timer--;
    if(timer < 0) clearInterval(comeca);
  }
}

var start = contagem()
var comeca = setInterval(start, 1000);

Let me know if it worked or if it went wrong.

Browser other questions tagged

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