admin管理员组

文章数量:1379395

Hi all i have got this really simple jquery counter

    var count = 10;
  countdown = setInterval(function(){
    $("p.countdown").html(count + " seconds remaining!");
    if (count == 0) {
     alert('done');
    }
    count--;
}, 1000);

How can i make it reset after it gets to 0 instead of going into minus???? so it keep iterating.

/

any help

Hi all i have got this really simple jquery counter

    var count = 10;
  countdown = setInterval(function(){
    $("p.countdown").html(count + " seconds remaining!");
    if (count == 0) {
     alert('done');
    }
    count--;
}, 1000);

How can i make it reset after it gets to 0 instead of going into minus???? so it keep iterating.

http://jsfiddle/isimpledesign/mSQdp/

any help

Share Improve this question edited Aug 10, 2011 at 13:42 Naftali 146k41 gold badges247 silver badges304 bronze badges asked Aug 10, 2011 at 13:39 DCHPDCHP 1,1316 gold badges30 silver badges54 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 3

To reset and repeat, put count = 11; after the alert('done');.

To stop, put clearInterval(countdown); after the alert('done');.

Try this

var count = 10;
  countdown = setInterval(function(){
    $("p.countdown").html(count + " seconds remaining!");
    count--;    
    if (count == 0) {
      count = 10;
    }

}, 1000);
var count = 10,
    countdown = setInterval(function () {
        $("p.countdown").html(count + " seconds remaining!");
        if (count == 0) {
            count = 11; //since it will be reduced right after this
            //clearInterval(countdown); <-- use this if you want to stop 
            alert('done');
        }
        count--;
    }, 1000);

Just like this:

if (count == 0)
  count = 11

You might also want to look at the jQuery countdown plugin

...
if (count == 0) {     
    alert('done');    
    count = 11;
}
...

本文标签: javascriptSimple jquery counterStack Overflow