最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout counter not working - Stack Overflow

programmeradmin2浏览0评论

This setTimeout function only runs once and then stops. I get no errors so I have no idea why it's happening.

count = 100;

counter = setTimeout('timer()', 100);

$('#reset').click(function() {
    count = 100;
    counter = setTimeout('timer()', 100);
})

function timer() {
    if (count <= 0) {
        clearTimeout(counter);
        alert('done');
    }
    $('#counter').html(count);
    count -= 1;
}

I tried a few different formulations of the setTimeout function, including setTimeout(timer(),100) and setTimeout(function() { timer() }, 100)

This setTimeout function only runs once and then stops. I get no errors so I have no idea why it's happening.

count = 100;

counter = setTimeout('timer()', 100);

$('#reset').click(function() {
    count = 100;
    counter = setTimeout('timer()', 100);
})

function timer() {
    if (count <= 0) {
        clearTimeout(counter);
        alert('done');
    }
    $('#counter').html(count);
    count -= 1;
}

I tried a few different formulations of the setTimeout function, including setTimeout(timer(),100) and setTimeout(function() { timer() }, 100)

Share Improve this question asked Mar 4, 2013 at 22:25 HatHat 1,7316 gold badges28 silver badges45 bronze badges 1
  • counter = setTimeout(timer, 100); is the preferred syntax – Brad M Commented Mar 4, 2013 at 22:28
Add a ment  | 

6 Answers 6

Reset to default 3

You should be using setInterval() which repeats a function call, not setTimeout(), which does it once. Also, don't use () in function name reference.

var count = 100;

var counter = setInterval('timer', 100);

$('#reset').click(function() {
    count = 100;
    counter = setInterval('timer', 100);
})

function timer() {
    if (count <= 0) {
        clearInterval(counter);
        alert('done');
    }
    $('#counter').html(count);
    count -= 1;
}

setTimeout works correctly but it is not what you are looking for. try setInterval instead. setInteval(function, delay)

Yes, that's what setTimeout does. It runs the code once.

You want to use the setInterval method to run the code repeatedly.

setTimeout() - executes a function, once, after waiting a specified number of milliseconds.

You probably would like to go for setInterval() which executes a function, over and over again, at specified time intervals.

Not sure what you're trying to achieve, and I don't understand the $('#reset').click (etc) constructs. Are these JQuery?

However, why not use setInterval()? And then clear the interval timer when your condition is met?

var count = 10;

function counter() {

    if ( count > 0 )
    {
        --count;
        var t2 = setTimeout( counter, 1000 );

        document.querySelector("#demo").innerHTML = count;
    }
    else
    {
        clearTimeout(t2);

        document.querySelector("#demo").innerHTML = "Done";
    }
}

var countdown_timeout = counter();
<p>Count: <b><span id="demo"></span></b></p>

发布评论

评论列表(0)

  1. 暂无评论