Pergunta

My JS code:

 var counter = 3;
    var timer = setInterval(function () {counter--;  if (counter == 0) {clearInterval(interval);}}, 1000);

    $('#msg').hide().html('Page will refresh in ' + timer).slideDown('fast');

Why is it not working? This is the reply I get: "Page will refresh in 14522"

All I am trying to make 3 second countdown. I don't want any actions after that. So what is the problem?Help!

Thanks.

Foi útil?

Solução

First, you want to show counter on the screen, not timer. Second, you need to update the message every time your counter changes:

var timer = setInterval(function () {
    counter--;
    $('#msg').html('Page will refresh in ' + counter);
    if (counter == 0) {
        clearInterval(interval);
    }
}, 1000);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top