0

I have a function in JavaScript. I use setInterval in order to control my function. I also have another logic for controlling my function. I have a counter which is increased once one condition happens and is decreased when another condition happens. Now, sometimes the second condition does not happen and hence my function won't be resume anymore. (I pause my function when my first condition happen). Therefore, I want to wait at most 30 seconds for the second condition. If it does not happen, then I want to resume my function anyway. I have following code, but it does not work as I expect it. What happens is that it resume my function every 30 seconds. Then, it may be resumed while it should wait. Can someone let me know what is the problem with my code?

Please note that, the value for the counter may increase to more than 20. I mean the first and second condition may occur more than once.

function main() 
{
    // body
}

function increaseCounter()
{
    counter += 1;

    clearInterval(controller);
    controlSecond = setInterval(function(){
        counterSeconds += 1;
        if (counterSeconds == 30)
        {
            counterSeconds = 0;

            controller = setInterval(main, 100);
            clearInterval(controlSecond);
        }
    }, 1000);
}

function decreaseCounter()
{
    counter -= 1;

    if (counter == 0)
    {
        counterSeconds = 0;
        clearInterval(controlSecond);

        controller = setInterval(main, 100);
    }
}
4
  • How are these 2 functions called? Can you put up a simple demo via jsfiddle? Commented Apr 5, 2015 at 3:29
  • To be honest it's a bit difficult since this is a small part of my big project! In fact I use PhantomJS and these two functions are two of its callbacks functions. Commented Apr 5, 2015 at 3:37
  • 1
    You have to put clearInterval(x); right before every x = setInterval(...) statement. Commented Apr 7, 2015 at 20:48
  • Thank you! this solved most of my problems. clearInterval right before setting that. Commented Apr 11, 2015 at 13:48

2 Answers 2

2

Consider what happens if you call increaseCounter twice in a row.

On the first execution it will create interval A and assign it to controlSecond.

On the second execution it will create interval B and assign it to controlSecond, while interval A continues to fire off indefinitely. You won't stop it with clearInterval(controlSecond) because controlSecond no longer references interval A.

The problem is that you continue to set controlSecond and controller to a new interval without clearing them first. That results in the intervals being leaked with no way of clearing them. It's sort of like a memory leak where you have dynamically allocated memory but nothing pointed at it, but instead of renegade memory you have renegade intervals.

One way to prevent this is to make sure you always clear your interval before setting it.

I would also recommend that you implement controlSecond with a setTimeout because that is designed for tasks which only happen once.

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

1 Comment

I understand your point which is correct! But, I don't want to use setTimeout because I want my functions to be resumed at the time they are ready. But, using setTimeout I have to wait for example for 3 seconds each time. That would be too much for some of actions and maybe less for some others. How can I clear my interval before setting them again?
0

Why not

var counter = 0
var timeout = null

function main () {
    clearTimeout(timeout);
    timeout = null;
}

function increaseCounter () {
    counter++;
    if (!timeout)
        timeout = setTimeout(main, 30*1000);
}

function decreaseCounter() {
    counter--;
    if (counter === 0) 
        main();
}

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.