2017-04-18 101 views
0

我有一个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); 
    } 
    }); 
} 
+0

删除重复的代码? – evolutionxbox

+0

使用'setTimeout()'代替 – mike510a

回答

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安排其下一个呼叫以前的异步结果又回来后才:如果你想尽量保持开始接近4000ms除了尽可能

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

    ,你可以当check_getqueue开始记剃去结果拿了回来时间:

    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))); 
         } 
        }); 
    } 
    
+0

Doh!如果你没有在#1上看到'return',请点击刷新。如果你看到'4000:'而不是'4000';',点击刷新。 (哇,我的眼睛今天给我带来了麻烦。) –

+0

你好,先生谢谢你的回答,我正在检查你的答案。 –

+0

你好先生,我需要总是使用setTimer,因为我创建了很多用户,我总是检查变量的状态,如果它被其他用户使用,那么为什么我总是需要它,我想要的是,有时,refresh_afterdel(clinicID,userID)正在运行2次。 –