打开页面上的所有链接并延迟

时间:2012-12-02 15:01:11

标签: jquery

我正在尝试打开页面上的所有链接,当href包含某些文本时,我正在使用

$('a[href*="/steve"]').each(function() {
  window.open($(this).attr('href') );   
});

现在我正在使用的页面包含很多与href中该文本的链接,所以它在很短的时间内打开了我的打开窗口,我想添加一个延迟,所以它有时间打开和在打开下一个之前等待大约5秒钟。我试图使用.delay,但无法让它工作,因为我很新,并且不知道确切的位置。

2 个答案:

答案 0 :(得分:2)

你可以这样做:

$('a[href*="/steve"]').each(function(index) {
    setTimeout(
         function(href){window.open(href)},
         (index+1)*5000, $(this).attr('href')
    );
});

我们的想法是以不断增加的延迟致电setTimeout

答案 1 :(得分:2)

使用setTimeout添加延迟:

var i = 0;
$('a[href*="/steve"]').each(function() {
    ++i;
    setTimeout(function(href) { window.open(href) },i*5000, $(this).attr("href"));
});
相关问题