Create a javascript timer [duplicate]

-1

I wonder if anyone can help me create a timer, a counter in the existing code. It's like 600 time, but I would like to set the time to 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>     
    
asked by anonymous 14.08.2018 / 13:26

1 answer

-1

Try to follow this template and adapt it 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 what went wrong.

    
14.08.2018 / 14:51