質問

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.

役に立ちましたか?

解決

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top