简体   繁体   English

jQuery setTimeOut:在for循环中

[英]jQuery setTimeOut: in for-loop

I'm having a little trouble with the setTimeOut-function. 我在使用setTimeOut函数时遇到了一些麻烦。 Although I saw other topics concerning this feature, I still don't get it. 尽管我看到了与此功能有关的其他主题,但仍然不了解。

I want to spawn 5 "orcs", but not at the same time. 我想生成5个“兽人”,但不能同时生成。 So I wanted to give each a delay of fe 5 seconds. 因此,我想给每个延迟5秒。 The best thing I created was an infinite loop, but I don't know why it didn't stop after 4. 我创建的最好的东西是一个无限循环,但是我不知道为什么它在4点后没有停止。

for (var z = 0; z < 4; z++) {
    (function (z) {
        setTimeout(function () {
            $("#gameBoard").append("<img class='Orc' src='Img/Orc.gif' alt='Orc'/>");
            $( ".Orc" ).animate({ "left": "+=100%" }, 20000 );
            console.log("test");
        }, 4000);
    }(z))
}

This will only spawn one Orc. 这只会产生一个兽人。 Why's this happening? 为什么会这样呢?

It doesn't have to be with a passed variable. 它不必与传递的变量一起使用。

Any ideas? 有任何想法吗?

If you want the delay to be different for each orc, you must pass the setTimeout a different delay per orc: 如果希望每个兽人的延迟都不同,则必须为每个兽人传递setTimeout一个不同的延迟:

for (var z = 0; z < 4; z++) {
    (function (z) {
        setTimeout(function () {
            $("#gameBoard").append("<img class='Orc' src='Img/Orc.gif' alt='Orc'/>");
            $( ".Orc" ).animate({ "left": "+=100%" }, 20000 );
            console.log("test");
        }, 4000 * z);
    }(z))
}

Notice that I am multiplying the delay by z . 注意,我将延迟乘以z

You can try with something like this: 您可以尝试使用以下方法:

var orcsSent = 0;

function sendOrc() {
    $("#gameBoard").append("<img class='Orc' src='Img/Orc.gif' alt='Orc'/>");
    $( ".Orc" ).animate({ "left": "+=100%" }, 20000 );
    console.log("test");

    if(orcsSent < 5) {
        orcsSent++;
        setTimeout(function () {
             sendOrc();
        }, 4000);
    }
}

sendOrc();

Use if and then call the function again. 使用if,然后再次调用该函数。

for example 例如

var step = 0
function SpawnOrcs(){
    [code for orc spawn]
    if (step <4)
    {
        step++
        setTimeout(SpawnOrcs,[time to wait in ms])
    }
}

For a cleaner implementation of @lpg's answer: 为了更清晰地实现@lpg的答案,请执行以下操作:

function sendOrcs(n) {
    (function loop() {
        if (--n < 0) return;
        $("#gameBoard").append("<img class='Orc' src='Img/Orc.gif' alt='Orc'/>");
        $( ".Orc" ).animate({ "left": "+=100%" }, 20000 );
        setTimeout(loop, 4000);
    })();
}

NB: this will spawn the first Orc immediately, not after 4 seconds. 注意:这将立即生成第一个兽人,而不是在4秒后。

What you need are Orc names! 您需要的是兽人的名字!

Try this: 尝试这个:

function spawnOrcs(numberOfOrcs) {

    var interval = 2000;
    var orcNames = ["Graklak Glasha", "Ougigoth Batul", "Zoughat Ushug", "Graman Lash", "Grushnag Sharn", "Urgan Bumph", "Raghat Durgat", "Xujarek Uloth", "Wumkbanok Bor", "Gnarlug Umog"];

    for (var i = 0; i < numberOfOrcs; i++) {
        var name = orcNames[i % orcNames.length];
        (function(i, name) {
            setTimeout(function() {
                console.log("Spawned Orc:", name);
            }, interval * (i + 1))
        })(i, name);
    }

}

spawnOrcs(5);

I'll wait patiently over here while you think about marking this as the best answer ever! 在您考虑将其标记为有史以来最好的答案时,我将在这里耐心等待。 :D :D

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

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