9

I have this loop that works okay:

function countdown(counter) {
    x = counter;
    if (x > 0) {
        x--;
        alert(x + "/5");
        if (x > 0) {
            setTimeout(function () {
                countdown(x);
            }, 500);
        }
    }
};

countdown(6);

But I'd like to be able to kill it, and start it again by pressing a button. I'd like to restart the loop, regardless of what number it's on when I press the button.

I've got nowhere over the last few hours.

Here's a jsfiddle: http://jsfiddle.net/4vtPH/4/

3
  • 1
    global state seems to be the only answer. Commented Jul 30, 2013 at 0:16
  • 1
    Save the return value of setTimeout somewhere. Call clearTimeout, then re-call countdown(6);. Commented Jul 30, 2013 at 0:18
  • Not sure, but setInterval might be better in your case? developer.mozilla.org/en-US/docs/Web/API/window.setInterval it also has a clear method similar to timeout Commented Jul 30, 2013 at 0:22

4 Answers 4

16

You can use clearTimeout (documentation)

var timeout;
function countdown(counter) {
    x = counter;
    if (x > 0) {
        x--;
        alert(x + "/5");
        if (x > 0) {
            timeout = setTimeout(function () {
                countdown(x);
            }, 2000);
        }
    }
};
$('input').click(function() {
    clearTimeout(timeout);
    countdown(6);
});
countdown(6);

Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, i have used same clearTimeout() function to clear the already running loop. but it still running already created instances. So, after using clearTimeout(), i try to create new instance. Instead of clearing the previous one, it is added as another concurrent loop. Can you please help me on destroy the previous instances?
0

You should really look at the documentation for setTimeout.

It returns a timeout id that you can use with clearTimeout to reset.

Comments

0

First you need to assign your setTimeout to a variable.

var timeout = setTimeout(..

Then when needed you can use clearTimeout(timeout). Make sure your timeout variable is scoped in a way that it can be accessed anywhere you need to access it.

Comments

0

How about using an interval for a countdown? Here is an alternative solution (DEMO):

function Countdown(counter) {
    this.interval = 0;
    this.x = counter;
    console.log(this.x + "/"+counter);
    this.start = function() {
        var self = this;
        this.interval = setInterval(function () {
            self.x--;
            console.log(self.x + "/"+counter);
            if(self.x == 0) self.stop();
        }, 2000);
    };

    this.stop = function() {
        clearInterval(this.interval);  
    };
};

var c = new Countdown(7);
c.start();

var c2 = new Countdown(4);

c2.start();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.