简体   繁体   English

如何在 while 循环中添加延迟。 JavaScript

[英]how to add a delay in a while loop. JavaScript

        function falling()
        {
            isFalling = true;

            while (isFalling == true)
            {

                if (y < 120) {

                    y++;

                }
                else if (y == 120) {
                    isFalling = false;

                }

            }

        }

I have tried adding setTimeout(function() around the entire loop, around the if statement, around the y++. I don't know what i'm doing wrong. Any time I add any of these the page becomes unresponsive once the falling function is called. I'm well aware that this is probably a duplicate question but the duplicate questions failed ti help. { }, 100)我试过在整个循环、if 语句周围、y++ 周围添加 setTimeout(function()。我不知道我做错了什么。任何时候我添加任何这些时,一旦下降函数,页面就会变得无响应被调用。我很清楚这可能是一个重复的问题,但重复的问题没有帮助。{ }, 100)

You would do it like this:你会这样做:

function falling(y) {
    if (y < 120) {
        setTimeout(falling.bind(this, y + 1), 100); // specify your delay.
    }
}

falling(0);

The question has indeed been answered several times, and the answers here are really not that different from this.这个问题确实已经回答过好几次了, 这里回答真的和这个没有太大区别。

Note that I removed the apparently global variable isFalling .请注意,我删除了明显的全局变量isFalling If you do need that variable in other code, then you can keep that variable updated as follows:如果您确实需要在其他代码中使用该变量,则可以按如下方式更新该变量:

function falling(y) {
    isFalling = y < 120;
    if (isFalling) {
        setTimeout(falling.bind(this, y + 1), 100); // specify your delay.
    }
}

I would use window.setInterval() instead, as you want it to repeat until a certain number我会使用 window.setInterval() 代替,因为您希望它重复直到某个数字

working plunkr:工作plunkr:

https://plnkr.co/edit/TbpplnIShiaJR7sHDUjP?p=preview https://plnkr.co/edit/TbpplnIShiaJR7sHDUjP?p=preview

function falling()
    {
        var isFalling = true; 
        var y=0;
        myInterval = window.setInterval(function() {
          y++;
          console.log(y);
          if (y== 120) {
            clearInterval(myInterval);
          }
        }, 100)
    }

falling();

If the intent of calling falling() is to temporarily set the value of a global variable to true (as suggested by your current code), then this should do the trick.如果调用falling()的目的是暂时将全局变量的值设置为true (如您当前的代码所建议的那样),那么这应该可以解决问题。

 var isFalling = false; function falling(fallDuration) { isFalling = true; console.log("now falling"); setTimeout(function(){ isFalling = false; console.log("landed"); }, fallDuration || 1000); } falling();

I imagine though that you might actually want to fall and then continue doing something else.我想虽然你可能真的想跌倒然后继续做其他事情。 In that case you probably want to look at a callback or into promises.在这种情况下,您可能想要查看回调或承诺。 You might do a simple:你可以做一个简单的:

 var isFalling = false; function falling(fallDuration, andThen) { isFalling = true; console.log("now falling"); setTimeout(function() { isFalling = false; console.log("landed"); andThen(); }, fallDuration); } falling(1000, function(){ console.log("now standing"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM