Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top