多个setTimeout和clearTimeout相互交错

时间:2018-05-16 09:54:01

标签: javascript jquery

我正在尝试完成以下操作。我有一个按钮的多个表单。当我点击那个按钮时,我想让它改变颜色和文字2秒钟。在该时间范围内再次单击时,它会重置并且旋转将重新开始。

到目前为止,我的代码按预期工作,但是,当我在这2秒内点击其他表单的按钮时,旋转停止,因为timerId已清除clearTimeout

看看我的例子,当你点击'order1'并在2秒内你点击'order2'然后'order1'不会改变回原来的状态。有没有办法让其他形式互不干扰?

var timerId, delay = 2000;

$(".product-form").submit(function(e){
 
var button = $(this).find('button[type=submit]'),
  order = button.find("span:eq(0)"),
  added = button.find("span:eq(1)");

  clearTimeout(timerId);
  added.hide();
  order.hide();
  button.addClass("green");
  added.slideDown("slow");

  timerId = setTimeout(function() {
    added.hide();
    order.show();
    button.removeClass("green");
  }, delay); 
  e.preventDefault();
});
form {
  float: left;
  padding: 10px;
}

.hide {
  display: none
}

#button {
  width: 200px;
  height: 40px;
  border: none;
  background: orange;
}

.green {
  background: green !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="product-form" action="#cart" method="post">
  <button id="button" type="submit">
    <span>Order1</span>
    <span class="hide">&#10003; Added</span>
  </button>
</form>
 
<form class="product-form" action="#cart" method="post">
  <button id="button" type="submit">
    <span>Order2</span>
    <span class="hide">&#10003; Added</span>
  </button>
</form>

<form class="product-form" action="#cart" method="post">
  <button id="button" type="submit">
    <span>Order2</span>
    <span class="hide">&#10003; Added</span>
  </button>
</form>

2 个答案:

答案 0 :(得分:1)

您的代码仅对所有按钮使用一个timerId,这就是为什么他们互相踩踏的原因。您可以在按钮上存储timerId,请参阅***行:

&#13;
&#13;
var delay = 2000; // *** No timerId here

$(".product-form").submit(function(e) {
  var $this = $(this),                               // ***
      timerId = $this.data("timerId"),               // ***
      button = $this.find('button[type=submit]'),    // ***
      order = button.find("span:eq(0)"),
      added = button.find("span:eq(1)");

  clearTimeout(timerId);
  added.hide();
  order.hide();
  button.addClass("green");
  added.slideDown("slow");

  $this.data("timerId", setTimeout(function() {      // ***
    added.hide();
    order.show();
    button.removeClass("green");
  }, delay));                                        // ***
  e.preventDefault();
});
&#13;
form {
  float: left;
  padding: 10px;
}

.hide {
  display: none
}

#button {
  width: 200px;
  height: 40px;
  border: none;
  background: orange;
}

.green {
  background: green !important;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="product-form" action="#cart" method="post">
  <button id="button" type="submit">
    <span>Order1</span>
    <span class="hide">&#10003; Added</span>
  </button>
</form>
 
<form class="product-form" action="#cart" method="post">
  <button id="button" type="submit">
    <span>Order2</span>
    <span class="hide">&#10003; Added</span>
  </button>
</form>

<form class="product-form" action="#cart" method="post">
  <button id="button" type="submit">
    <span>Order2</span>
    <span class="hide">&#10003; Added</span>
  </button>
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您只需进行3次简单的更改:

  1. timerId成为数组;
  2. 删除clearTimeout声明;
  3. 由于timerId现在是一个数组,因此请使用timerId[timerId.length] = setTimeout(function() {
  4. 工作示例:

    var timerId = [];
    var delay = 2000;
    
    $(".product-form").submit(function(e){
     
    var button = $(this).find('button[type=submit]'),
      order = button.find("span:eq(0)"),
      added = button.find("span:eq(1)");
    
      added.hide();
      order.hide();
      button.addClass("green");
      added.slideDown("slow");
    
      timerId[timerId.length] = setTimeout(function() {
        added.hide();
        order.show();
        button.removeClass("green");
      }, delay); 
      e.preventDefault();
    });
    form {
      float: left;
      padding: 10px;
    }
    
    .hide {
      display: none
    }
    
    #button {
      width: 200px;
      height: 40px;
      border: none;
      background: orange;
    }
    
    .green {
      background: green !important;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form class="product-form" action="#cart" method="post">
      <button id="button" type="submit">
        <span>Order1</span>
        <span class="hide">&#10003; Added</span>
      </button>
    </form>
     
    <form class="product-form" action="#cart" method="post">
      <button id="button" type="submit">
        <span>Order2</span>
        <span class="hide">&#10003; Added</span>
      </button>
    </form>
    
    <form class="product-form" action="#cart" method="post">
      <button id="button" type="submit">
        <span>Order3</span>
        <span class="hide">&#10003; Added</span>
      </button>
    </form>