foreach循环的每次迭代之间的延迟? [英] Delay between each iteration of foreach loop?

查看:381
本文介绍了foreach循环的每次迭代之间的延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作一个西蒙说游戏。此功能显示当前序列。它现在的问题在于它并没有真正顺利进行,它可以同时完成所有事情。假设颜色是蓝色,红色和黄色,它们会同时发生而不是按顺序发生。我该如何解决这个问题?

so I am making a simon says game. This function displays the current sequence. The problem with it right now is that it doesn't really go in a nice sequence, it kind of does everything at once. Say the colors are "blue", "red", and "yellow, they will all go off at the same time rather than in sequence. How can I fix this?

var displaySequence = function(){
    compSequence.forEach(function(color){
        $("#" + color).fadeTo(300, 0.5).fadeTo(300, 1.0);
    })
}


推荐答案

一个无jQuery解决方案。你需要使用数组索引来给出每个调用之间等待的错觉,但是每个函数都已经运行了。会发生什么:显示颜色1合1秒,显示颜色2秒2 ...

A none jQuery solution. You will need to use the array index to give the illusion of waiting between each call, however each function has ran already. What will happen is: show color 1 in 1 second, show color 2 in 2 seconds...

var displaySequence = function(){
    compSequence.forEach(function(color, index){
        setTimeout(function(){
            $("#" + color).fadeTo(300, 0.5).fadeTo(300, 1.0);
        },
        1000 * index);
    })
}

调整1000 *索引来改变延迟。

adjust the 1000 * index to change the delay.

这篇关于foreach循环的每次迭代之间的延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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