简体   繁体   English

循环结束前执行循环后代码

[英]Code After Loop is Executing Before Loop Ends

I am working on improving my coding skills and came up with a project to complete. 我正在努力提高自己的编码技能,并提出了一个项目来完成。 It is currently a program where you choose up to 5 directional choices using buttons. 当前是一个程序,您可以使用按钮最多选择5个方向选择。 Pressing the directional buttons adds that direction to an array. 按下方向按钮可将该方向添加到阵列中。 I created a function, startMove() to move a ball on the screen based on the directions in the array. 我创建了一个函数startMove()以根据数组中的方向在屏幕上移动一个球。 When it is executed, the program moves the ball one direction at a time while pausing in between. 执行该程序时,程序一次将球向一个方向移动,同时在两者之间暂停。 I also have a section where an arrow will show up for each direction you input to show what you have queued up. 我也有一个部分,其中您输入的每个方向都会显示一个箭头,以显示您排队的内容。 You can view through JSFiddle . 您可以通过JSFiddle查看。

var startMove = function() {
for(var j=0;j<queue.length;j++) {
    spot = queue[j];
    if(spot=="left") {
        (function (j) {
            setTimeout(function () {
                switch(j) {
                    case 0:
                        document.getElementById("one").style.transform = "scale(1.5)";
                        break;
                    case 1:
                        document.getElementById("two").style.transform = "scale(1.5)";
                        break;
                    case 2:
                        document.getElementById("three").style.transform = "scale(1.5)";
                        break;
                    case 3:
                        document.getElementById("four").style.transform = "scale(1.5)";
                        break;
                    case 4:
                        document.getElementById("five").style.transform = "scale(1.5)";
                        break;
                    default:
                        console.log("Length is zero.");
                        break;
                }
                moveCharacter(left);
                console.log("X: "+character.x);
                console.log("Y: "+character.y);
            }, 1000*j);
        })(j);
    }
    else if(spot=="right") {
        (function (j) {
            setTimeout(function () {
                switch(j) {
                    case 0:
                        document.getElementById("one").style.transform = "scale(1.5)";
                        break;
                    case 1:
                        document.getElementById("two").style.transform = "scale(1.5)";
                        break;
                    case 2:
                        document.getElementById("three").style.transform = "scale(1.5)";
                        break;
                    case 3:
                        document.getElementById("four").style.transform = "scale(1.5)";
                        break;
                    case 4:
                        document.getElementById("five").style.transform = "scale(1.5)";
                        break;
                    default:
                        console.log("Length is zero.");
                        break;
                }
                moveCharacter(right);
                console.log("X: "+character.x);
                console.log("Y: "+character.y);
            }, 1000*j);
        })(j);
    }
    else if(spot=="up") {
        (function (j) {
            setTimeout(function () {
                switch(j) {
                    case 0:
                        document.getElementById("one").style.transform = "scale(1.5)";
                        break;
                    case 1:
                        document.getElementById("two").style.transform = "scale(1.5)";
                        break;
                    case 2:
                        document.getElementById("three").style.transform = "scale(1.5)";
                        break;
                    case 3:
                        document.getElementById("four").style.transform = "scale(1.5)";
                        break;
                    case 4:
                        document.getElementById("five").style.transform = "scale(1.5)";
                        break;
                    default:
                        console.log("Length is zero.");
                        break;
                }
                moveCharacter(0, up);
                console.log("X: "+character.x);
                console.log("Y: "+character.y);
            }, 1000*j);
        })(j);
    }
    else if(spot=="down") {
        (function (j) {
            setTimeout(function () {
                switch(j) {
                    case 0:
                        document.getElementById("one").style.transform = "scale(1.5)";
                        break;
                    case 1:
                        document.getElementById("two").style.transform = "scale(1.5)";
                        break;
                    case 2:
                        document.getElementById("three").style.transform = "scale(1.5)";
                        break;
                    case 3:
                        document.getElementById("four").style.transform = "scale(1.5)";
                        break;
                    case 4:
                        document.getElementById("five").style.transform = "scale(1.5)";
                        break;
                    default:
                        console.log("Length is zero.");
                        break;
                }
                moveCharacter(0, down);                    
                console.log("X: "+character.x);
                console.log("Y: "+character.y);
            }, 1000*j);
        })(j);
    }
}
//removeAll();

}; };

Currently the program works almost perfectly. 目前,该程序几乎可以完美运行。 I even have it so that the arrows grow in size as they are being executed. 我什至拥有它,以使箭头在执行时会变大。 The section above is the problem. 上面的部分是问题。 I wanted to edit the startMove() function so that the very last thing it does is hide all the images again and clears the array. 我想编辑startMove()函数,以便它做的最后一件事是再次隐藏所有图像并清除数组。 I have a function that does this already, removeAll(). 我有一个已经执行此操作的函数removeAll()。 I add the call to removeAll() function at the very end of the startMove() function, yet when I run the program, queue up my options, and execute, the ball moves as it's supposed to, but the arrows disappear after the first movement. 我在startMove()函数的末尾添加了对removeAll()函数的调用,但是当我运行程序,对我的选项进行排队并执行时,球按预期的方向移动,但是箭头在第一个之后消失运动。 Why does removeAll() run before the movement is finished? 为什么removeAll()在移动完成之前运行? Does it not recognize the pauses I included? 它不识别我包括的暂停吗?

Any help would be appreciated. 任何帮助,将不胜感激。 I still have a lot to learn so if anything else in my code could be done better I would love to hear feedback on that as well. 我还有很多东西要学习,所以如果我的代码中的其他任何事情都可以做得更好,我也希望听到对此的反馈。 I currently have the call to removeAll() commented out so you can see how the code runs without it. 我目前已将对removeAll()的调用注释掉,以便您查看没有它的代码如何运行。 It is one of the last lines of the javascript. 这是javascript的最后几行之一。

The problem is because your pauses, are actually delayed execution of seperate processes. 问题是因为您的暂停实际上是延迟了单独进程的执行。 setTimeout accepts a callback (function) as the first parameter. setTimeout接受回调(函数)作为第一个参数。 It waits the number of milliseconds speficied by the second parameter, and then calls the callback function. 它等待第二个参数指定的毫秒数, 然后调用回调函数。 But it does this in a non-blocking way . 但这是以非阻塞的方式进行的

setTimeout(() => console.log("Hello"),5000); // waits 5 seconds then prints 'Hello'
console.log(" World!") // prints ' World' right after the timeout has been set.
// but without waiting the five seconds.

Think of it like setting a timer to take cookies out of the oven, but then pouring a glass of milk while you wait, or in code. 可以将其想象为设置计时器以将饼干从烤箱中取出,然后在等待时或在代码中倒入一杯牛奶。

setTimeout(takeCookiesOutOfOven, 30000);
pourGlassOfMilk(); //You don't need to wait for the cookies to do this, so you just do it.

If you want to wait for pauses before executing the next line of code, Look into the async/await and promise modifications made to setTimeout here: Combination of async function + await + setTimeout 如果要在执行下一行代码之前等待暂停,请查看async / await并在此处承诺对setTimeout进行修改: async函数+ await + setTimeout的组合

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

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