防止setInterval函数运行两次

时间:2017-04-18 09:11:20

标签: javascript jquery setinterval

我有一个setInterval函数,每隔一小时运行一次。现在,我正在浏览器中浏览我的控制台,我看到了,我的setInterval函数中的函数有时会运行两次。我怎样才能阻止它运行两次?

这是我的setinterval:

$('#myclinic_id').change(function(){
    clearInterval(interval);
    lastQueueID = 0;
    $("#boxqueue").empty();
    var selectedClinicID = $(this).val();
    clinicID = selectedClinicID; 
    statusClinic(clinicID, userID);
    show_patients(clinicID, userID);

    if(selectedClinicID != "0" || selectedClinicID != undefined){
    interval = setInterval(function(){
    check_getqueue(clinicID, userID);
    }, 4000);  
    }
});$('#myclinic_id').change(function(){
    clearInterval(interval);
    lastQueueID = 0;
    $("#boxqueue").empty();
    var selectedClinicID = $(this).val();
    clinicID = selectedClinicID; 
    statusClinic(clinicID, userID);
    show_patients(clinicID, userID);

    if(selectedClinicID != "0" || selectedClinicID != undefined){
    interval = setInterval(function(){
    check_getqueue(clinicID, userID);
    }, 4000);  
    }
});

现在,在check_getqueue函数内部我还有一个函数,我想阻止它运行两次,这是我的问题发生。这是我在check_getqueue函数中的代码,其中check_getqueue函数中的函数名为refresh_afterdel(clinicID, userID);,以防止运行两次。

以下是check_getqueue的完整代码:

function check_getqueue(clinicID, userID) {
var tmpCountQ = [];
  $.ajax({
    url: siteurl+"sec_myclinic/checkingUpdates/"+clinicID+"/"+userID,
    type: "POST",
    dataType: "JSON",
    success: function(data) {
      for(var i=0;i<data.length;i++) {
        tmpCountQ.push(data[i]['queue_id']);
      };
      if(typeof lastCon[0] != "undefined")
      {
        for(j=0;j < tmpCountQ.length;j++)
        {
          if(tmpCountQ[j] != lastCon[j])
          {
            refresh_afterdel(clinicID, userID);
            lastCon[j] = tmpCountQ[j];
          } 
        }
      }
      else
      {
       lastCon = tmpCountQ;
      }
      // console.log("lastCon "+lastCon)
      // console.log("tmpCountQ "+tmpCountQ);
    }
  });
}

1 个答案:

答案 0 :(得分:2)

这取决于您希望如何安排它。您的check_getqueue函数不是与字面重叠,只是函数启动异步进程然后返回;该过程直到稍后才完成,有时(显然)在下一次调用check_getqueue开始下一个异步过程之前尚未完成。

您的两个基本选择是:

  1. 使用保护变量,并在设置变量时忽略对check_getqueue的任何调用:

    var check_getqueue_ignore = false;
    function check_getqueue() {
        if (check_getqueue_ignore) {
            return;
        }
        check_getqueue_ignore = true;
        $.ajax({
            // ...
            complete: function() {
                check_getqueue_ignore = false;
            }
        });
    }
    
  2. 根本不要使用setInterval;相反,只有在先前的异步结果返回后才check_getqueue安排其下一个呼叫:

    timer = setTimeout(check_getqueue, 4000);
    // ...
    
    function check_getqueue() {
        $.ajax({
            // ...
            complete: function() {
                timer = setTimeout(check_getqueue, 4000);
            }
        });
    }
    

    如果你想让启动尽可能接近4000毫秒,你可以记住check_getqueue何时启动并减少结果回来的时间:< / p>

    timer = setTimeout(check_getqueue, 4000);
    // ...
    
    function check_getqueue() {
        var started = Date.now();
        $.ajax({
            // ...
            complete: function() {
                timer = setTimeout(check_getqueue, Math.max(0, 4000 - (Date.now() - started)));
            }
        });
    }
    
相关问题