Skip to content Skip to sidebar Skip to footer

Javascript - What Is Wrong With This While Loop? Never Ending Loop

I wanted to make a JSFiddle but the code crashes your tab/window... What is wrong with this while loop? It looks right to me... var commentLoop = 150; var whileLoop = t

Solution 1:

Because Javascript is single threaded and event driven, as long as your while loop is running, the setInterval() can never fire and its callback will never get called. So you have a deadlock where your while loop just runs forever.

With the event driven nature of Javascript, the setInterval() puts an event into the event queue, but because your while loop never stops running, the interpreter never gets to the point where it can actually finish the current thread of JS execution and pull the next event out of the event queue to run the timer callback.

You cannot use while loop to wait for some other event to happen. In Javascript, the other event can't happen until the while loop itself is done. Instead, you need to use only timers and no while loop. If you can explain a little more clearly what real problem you're trying to solve, we can suggest a coding solution.

To add to the things that are wrong here, you're creating a new setInterval() timer every time through the while loop (so they will pile up with zillions of timers active) so that's messed up too.

You don't say exactly what you're trying to accomplish, but it appears you can use only the interval timer and not the while loop. So, assuming your want to run some operation 150 times, spaced 500ms apart, you could use this:

var count = 0;
var commentLoop = 150;
var timer = setInterval(function() {

    // regular code here// check if we're done repeatingif (count === commentLoop - 1) {
        // stop the timerclearInterval(timer);
    } else {
        count++;
    }

}, 500);

Or, this could go into a utility function (you can run this snippet to see it work):

functionrepeat(numTimes, delay, fn) {
    var cntr = 0;
    var timer = setInterval(function() {
        if (cntr >= numTimes) {
            clearInterval(timer);
        } else {
            // if callback returns true, then stop the timerif (fn(cntr) === true) {
                clearInterval(timer);
            }
            ++cntr;
        }
    }, delay);
}

// sample usagerepeat(5, 400, function(cnt) {
    console.log(cnt);
});

Post a Comment for "Javascript - What Is Wrong With This While Loop? Never Ending Loop"