Javascript 防止setInterval函数运行两次

Javascript 防止setInterval函数运行两次,javascript,jquery,setinterval,Javascript,Jquery,Setinterval,我有一个setInterval函数,它每分钟运行一次。现在,我在浏览器中浏览我的控制台,我看到setInterval函数中的函数有时运行两次。如何防止运行两次 以下是我的设置间隔: $('#myclinic_id').change(function(){ clearInterval(interval); lastQueueID = 0; $("#boxqueue").empty(); var selectedClinicID = $(this).val();

我有一个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\u getqueue
函数中,我还有一个函数,我想防止它运行两次,这里是我的问题发生的地方。这是我在check_getqueue函数中的代码,其中名为
refresh_afterdel(clinicID,userID)的函数在check_getqueue函数中,我不想阻止运行两次

以下是我的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);
    }
  });
}
function check\u getqueue(临床ID、用户ID){
var tmpCountQ=[];
$.ajax({
url:siteurl+“sec_myclinic/checkingUpdates/”+clinicID+“/”+userID,
类型:“POST”,
数据类型:“JSON”,
成功:功能(数据){

对于(var i=0;i,这取决于您希望如何安排它。您的
check_getqueue
函数实际上并没有与自身重叠,只是函数启动一个异步进程,然后返回;该进程直到稍后才完成,有时(显然)在下一次调用
check\u getqueue
启动下一个异步进程之前尚未完成

您的两个基本选择是:

  • 设置变量时,使用保护变量并忽略对
    check\u 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;
            }
        });
    }
    
  • 根本不要使用
    setInterval
    ;而是让
    check\u getqueue
    仅在上一个异步结果返回后安排下一次调用:

    timer = setTimeout(check_getqueue, 4000);
    // ...
    
    function check_getqueue() {
        $.ajax({
            // ...
            complete: function() {
                timer = setTimeout(check_getqueue, 4000);
            }
        });
    }
    
    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)));
            }
        });
    }
    
    如果您想尽量使启动间隔接近4000ms,您可以记住何时启动
    check_getqueue
    ,并缩短返回结果所需的时间:

    timer = setTimeout(check_getqueue, 4000);
    // ...
    
    function check_getqueue() {
        $.ajax({
            // ...
            complete: function() {
                timer = setTimeout(check_getqueue, 4000);
            }
        });
    }
    
    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)));
            }
        });
    }
    

  • 删除重复的代码?使用
    setTimeout()
    insteadoh!如果在#1上看不到
    返回
    ,请点击刷新。如果看到
    4000:
    而不是
    4000);
    ,请点击刷新。(哇,我的眼睛今天有问题。)您好先生谢谢您的回答我现在正在检查您的答案。您好先生,我需要始终使用setTimer,因为我创建了许多用户,我总是检查变量的状态,如果其他用户正在使用它,那么这就是为什么我总是需要它,我想要的是,有时会有刷新后的del(clinicID,userID)正在运行2次。您好,先生。我们可以讨论一下吗?