如何将setTimeout和clearTimeout设置为在同一按钮上起作用

时间:2018-12-05 14:40:58

标签: javascript jquery onclick

这是我的代码

$('#btnDelete').unbind().click(function(){
    var time = setTimeout(function() {
        $.ajax({
            type: 'ajax',
            method: 'get',
            async: false,
            url: '<?php echo base_url() ?>main/delete',
            data: {id:id},
            dataType: 'json',
            success: function(response){
                if(response.success){
                    showNews();
                } else {
                    alert('Error');
                }
            },
            error: function(){
                alert('Error deleting')
            }
        });
    }, 10000);
});

我在哪里可以放置clearTimeout()函数,以便每次单击同一按钮时计时器都会重置?谢谢!

2 个答案:

答案 0 :(得分:1)

尝试一下:

  var time;
  $('#btnDelete').unbind().click(function() {
    if (time) {
      clearTimeout(time);
      time = null;
    }
    time = setTimeout(function() {
      $.ajax({
        type: 'ajax',
        method: 'get',
        async: false,
        url: '<?php echo base_url() ?>main/delete',
        data: {
          id: id
        },
        dataType: 'json',
        success: function(response) {
          if (response.success) {
            showNews();
          } else {
            alert('Error');
          }
        },
        error: function() {
          alert('Error deleting')
        }
      });
    }, 10000);
  });

time设置为全局变量,或者在点击处理程序之外进行访问。检查时间值,如果有时间值则将其重置,否则您的常规代码将起作用。

答案 1 :(得分:0)

使用数据属性保存计时器的状态。您可以使用data属性来确定是否需要取消它。您将超时ID存储到data属性中,然后单击以对其进行检查。

$('#btnDelete').unbind().click(function(){
   var btn = $(this);  // reference the button that was clicked
   if (btn.data("timer")) {  // determine if it was already clicked
      window.clearTimeout(btn.data("timer"))  // cancel the timeout
      btn.data("timer", null);  // toggle it to not being used
   } else {
     var time = setTimeout(function() {
       btn.data("timer", null);  // reset it if you want
       /* your code here */
     }, 10000)
     btn.data("timer", time);  // store the timeout id for next click
   }
});

如果您要重新启动它,则不要执行else,并且不需要将data属性设置为null,因为您将覆盖它。

$('#btnDelete').unbind().click(function(){
   var btn = $(this);
   if (btn.data("timer")) {
      window.clearTimeout(btn.data("timer"))
   }
   var time = setTimeout(function() {
       btn.data("timer", null);  // reset it if you want
       /* your code here */
   }, 10000)
   btn.data("timer", time);
});

在多个按钮上运行的示例

$('.btn-delete').unbind().click(function(){
   var btn = $(this);  // reference the button that was clicked
   if (btn.data("timer")) {  // determine if it was already clicked
      window.clearTimeout(btn.data("timer"))  // cancel the timeout
      btn.data("timer", null);  // toggle it to not being used
   } else {
     var time = setTimeout(function() {
       btn.data("timer", null);  // reset it if you want
       btn.text((new Date()).toLocaleString())
     }, 3000)
     btn.data("timer", time);  // store the timeout id for next click
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn-delete">Delete 1</button>
<button class="btn-delete">Delete 2</button>
<button class="btn-delete">Delete 3</button>
<button class="btn-delete">Delete 4</button>