我有一个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);
}
});
}
删除重复的代码? – evolutionxbox
使用'setTimeout()'代替 – mike510a