javascript-while循环有什么问题?永无止境的循环 [英] javascript - What is wrong with this while loop? never ending loop

查看:77
本文介绍了javascript-while循环有什么问题?永无止境的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个JSFiddle,但是代码使您的选项卡/窗口崩溃了.while循环出了什么问题?在我看来不错...

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 = true;
    var count = 0;

    while (whileLoop === true) {
        setInterval(function() {
            console.log("my regular code goes here");
            if (count == commentLoop - 1) {
                console.log("just entered while loop thing");
                whileLoop = false;
            }
            count++;
        }, 500);
    }

我想念什么?感谢您的帮助.

What am I missing? Thanks for the help.

推荐答案

由于Javascript是单线程的并且是事件驱动的,因此只要您的while循环正在运行,setInterval()就永远不会触发,并且其回调也不会被调用.这样一来,您的while循环将永远运行,您将陷入僵局.

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.

由于Java具有事件驱动的特性,因此setInterval()会将事件放入事件队列中,但是由于您的while循环永远不会停止运行,因此解释器永远也无法到达可以实际完成当前线程的地步. JS执行并将下一个事件从事件队列中拉出以运行计时器回调.

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.

您不能使用while循环来等待其他事件的发生.在Javascript中,除非完成while循环本身,否则其他事件不会发生.相反,您只需要使用计时器,而不必使用while循环.如果您可以更清楚地说明您要解决的实际问题,我们可以建议一种编码解决方案.

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.

要补充这里出错的内容,您每次都要通过while循环创建一个新的setInterval()计时器(这样它们就会堆积成千上万个活动的计时器),所以也搞砸了.

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.

您没有确切说出要完成的任务,但是看来您只能使用间隔计时器,而不能使用while循环.因此,假设您要运行150次,间隔500毫秒,可以使用以下操作:

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 repeating
    if (count === commentLoop - 1) {
        // stop the timer
        clearInterval(timer);
    } else {
        count++;
    }

}, 500);

或者,这可以进入实用程序功能(您可以运行此代码段以查看其工作原理):

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

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

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

这篇关于javascript-while循环有什么问题?永无止境的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆