2017-04-18 548 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次。 –