jquery每次延迟都没有效果

Joh*_*n C 1 each jquery delay settimeout

我试图使用每个函数,并在每次迭代的执行之间暂停.具体来说,我希望脚本在新窗口中打开一堆URL,但我希望每个窗口之间有2秒的暂停.现在,每个链接都打开,两者之间没有暂停.以下是我的代码.我不知道如何使用delay()函数,因为我不会在延迟后调用另一个jQuery效果.我也试过setTimeout无济于事.我错过了什么?

    $('.url').each(function() {
        url = $(this).attr("href");
        window.open('http://www.google.com' + url);
    });
Run Code Online (Sandbox Code Playgroud)

mVC*_*Chr 6

您需要使用.each()DOCS方法的index参数,以便将setTimeout延迟乘以项目的索引.这是因为每个循环中的迭代都会立即处理,因此您基本上可以设置0,2000,4000,6000等的延迟:

$('.url').each(function(i) {
    var url = $(this).attr("href");
    setTimeout(function() {
      window.open('http://www.google.com' + url);
    }, 2000*i);
});
Run Code Online (Sandbox Code Playgroud)