Jquery 延迟每个循环内函数执行的调用

Jquery 延迟每个循环内函数执行的调用,jquery,Jquery,我想为数组中的每个项执行一个函数。但是,我不希望函数同时执行。我想让它说为第一个项目执行函数,然后等待2秒钟,然后再为第二个元素执行函数。我怎么能这么做我不太确定。谢谢 $.each(sliderArray, function(index, value) { console.log("array value is "+this); openQv(this); }); 尝试使用setTimeout(),最重要的是不要忘记在每次迭代中创建一个作用域 $.each(

我想为数组中的每个项执行一个函数。但是,我不希望函数同时执行。我想让它说为第一个项目执行函数,然后等待2秒钟,然后再为第二个元素执行函数。我怎么能这么做我不太确定。谢谢

$.each(sliderArray, function(index, value) {
       console.log("array value is "+this);
       openQv(this);
   });

尝试使用
setTimeout()
,最重要的是不要忘记在每次迭代中创建一个作用域

$.each(sliderArray, function(index, value) {
    var $this = this;
    (function(i){ 
       setTimeout(function(){ openQv($this) }, i*2000) 
    })(index);
});

尝试使用
setTimeout()
,最重要的是不要忘记在每次迭代中创建一个作用域

$.each(sliderArray, function(index, value) {
    var $this = this;
    (function(i){ 
       setTimeout(function(){ openQv($this) }, i*2000) 
    })(index);
});
setTimeout()
就足够了吗

$.each(sliderArray, function(index, value) {
       var $this = index;
        setTimeout(function()
        {
          console.log("array value is " + $this);
          openQv($this);
        },2000); 
   });
setTimeout()
就足够了吗

$.each(sliderArray, function(index, value) {
       var $this = index;
        setTimeout(function()
        {
          console.log("array value is " + $this);
          openQv($this);
        },2000); 
   });

如果您希望延迟串联运行,您可以尝试以下方法:

function open(items, delayBetweenItems) {
    function handleItem(itemIndex) {
        // open the item in question
        openQv(items[ itemIndex ]);

        var nextItemIndex = itemIndex + 1;

        // ensure it exists
        if (!items[ nextItemIndex ]) {
            return;
        }

        setTimeout(function() {
            handleItem(nextItemIndex);
        }, delayBetweenItems);
    }

    handleItem(0);
}

open(sliderArray, 200);

这将在处理每个项目之间等待200毫秒,这样您将获得渐进效果

如果您希望延迟连续运行,您可以尝试以下操作:

function open(items, delayBetweenItems) {
    function handleItem(itemIndex) {
        // open the item in question
        openQv(items[ itemIndex ]);

        var nextItemIndex = itemIndex + 1;

        // ensure it exists
        if (!items[ nextItemIndex ]) {
            return;
        }

        setTimeout(function() {
            handleItem(nextItemIndex);
        }, delayBetweenItems);
    }

    handleItem(0);
}

open(sliderArray, 200);

这将在处理每个项目之间等待200毫秒,这样您将获得渐进效果

setTimeout()和recursionsetTimeout()以及recursion哇,您的回答很快,很好:)我得到了语法错误:缺失)在parenthetical@user1937021缺少一个右括号。更正了。假设我没有定义now@user1937021
(索引)
通过索引而不是
i
哇,你回答得很快,很好:)我得到了语法错误:缺失)在parenthetical@user1937021缺少一个右括号。更正了。假设我没有定义now@user1937021
(index)
通过index而不是
i
你能发布你的openQv函数吗?你能发布你的openQv函数吗?