使用Javascript点击多个链接在同一类,但与延迟之间的每个不同的链接点击

Using Javascript To Click Multiple Links In Same Class But With Delay Between Each Different Link Clicked

本文关键字:链接 延迟 之间 Javascript 使用 一类      更新时间:2023-09-26
$(function(){
    window.location.href = $('.links').attr('href');
});

类名称是"链接",下面有很多链接,我如何添加延迟到每个链接点击,而不是有脚本点击相同的链接一次又一次?

谢谢

$(function () {
  // Grab all links into a wrapped set.
  var $links = $('.link');
  // Start with the first link at index 0 in the wrapped set.
  var linkNum = 0;
  // Specify our delay time.
  var delay = 1; // seconds
  // Setup a click handler on all our links.
  $links.click(function (e) {
    var $this = $(this);
    console.log($this.text() + ' clicked.');
    $this.css({ color: '#f00' });
    // After our delay, click the next link.
    setTimeout(function () {
      ++linkNum;
      if (linkNum < $links.length) {
        $links.eq(linkNum).click();
      } else {
        linkNum = 0;
        console.log("All links clicked.");
      }
    }, delay * 1000);
  });
  $('button').click(function () {
    $links.eq(0).click();
  });
});

http://codepen.io/Chevex/pen/WvzXbQ?编辑= 101