문제

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