2017-04-18 8 views
0

나는 초 단위로 실행되는 setInterval 함수를 가지고 있습니다. 이제 브라우저에서 내 콘솔을 탐색하고 있었는데 setInterval 함수 내부의 함수가 두 번 실행되는 경우가있었습니다. 두 번 실행하지 못하게하려면 어떻게해야합니까? 여기에, 이제 check_getqueue 함수 내에서 나는 또한 내가 두 번 실행을 방지하고자하는 기능을 가지고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); 
    } 
}); 

내 문제가 발생할 수 있습니다 :

여기 내에서는 setInterval입니다. 다음은 check_getqueue 함수 안에있는 코드입니다. check_getqueue 함수 안에 refresh_afterdel(clinicID, userID);이라는 함수를 두 번 실행하지 않아도됩니다.

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 함수 이 문자 그대로 자신과 겹치지 않으므로 함수 이 비동기 프로세스 인을 시작한 다음 반환합니다. 이 프로세스는 나중에 완료 될 때까지 완료되지 않으며 다음 번 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); 
         } 
        }); 
    } 
    

    당신이 시작 떨어져 가능한 4000ms에 가까운을 유지하려고 시도하는 경우 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'이 보이지 않는다면 refresh를 누르십시오. '4000 :'대신'4000 :');이 표시되면 refresh를 누르십시오. (와우, 오늘 내 눈이 내게 문제를주고있다.) –

+0

안녕하십니까, 당신의 대답에 대해 감사합니다. –

+0

hello sir, 나는 많은 사용자를 만들었 기 때문에 setTimer를 항상 사용해야합니다. 변수의 상태를 항상 확인하고 있습니다. 다른 사용자가 사용했다면, 내가 항상 필요로하는 이유는 모두 필요합니다. 때로는 refresh_afterdel (clinicID, userID)이 2 번 실행됩니다. –