如何在点击之间延迟?

时间:2016-08-03 21:55:44

标签: javascript jquery

我正在模拟单击页面中所有按钮的过程,现在我想通过使用以下代码在每次单击之间有一些延迟,但由于某种原因它拒绝工作并且没有单击按钮:



var time = 500;
$("button").each(function() {
  setTimeout(function() {
    $(this).click();
  }, time);
  time += 500;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <title>TEST</title>
</head>

<body>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
</body>

</html>
&#13;
&#13;
&#13;

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
var time = 500;
$("button").each(function() {
  setTimeout(function() {
    $(this).click();
  }.bind(this), time);
  time += 500;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <title>TEST</title>
</head>

<body>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
  <button type="button">Click Me!</button>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我不完全确定你想要达到什么目标,因为你的代码并没有做很多事情......我能想象的是,当你按下一个按钮并且每次按下后增加时间时你想要做某事按......如果是的话,你去吧:

var time = 500;

$(document).ready(function() {
  $('button').click(function() {
    $(this).delay(time).queue(function(n) {
      $(this).text("I've been clicked!");
      time += 500;
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button">Click Me!</button>
<button type="button">Click Me!</button>
<button type="button">Click Me!</button>