我们正在使用轮询功能,但有4个地方我们必须确保轮询正在运行。我们如何确定当前的轮询实例是否正在运行,以便我们不创建另一个轮询实例并重叠轮询?
function longPoll(){
// do the request
chrome.storage.local.get("userAuth", function(data) {
if(data.hasOwnProperty('userAuth')){
if(!localStorage.disableNotifications){
checkUnread();
}
}
});
setTimeout(function(){
longPoll();
console.log('polling: '+new Date().getTime());
}, 5000);
};
答案 0 :(得分:2)
您可以设置一个布尔值var来跟踪轮询当前是否正在运行。像这样的东西:
var polling = false;
function longPoll(){
//do nothing if already polling
if( polling )
{
return;
}
//set polling to true
polling = true;
//rest of function code goes here...
//set polling to false after process is finished
polling = false;
setTimeout(function(){
longPoll();
console.log('polling: '+new Date().getTime());
}, 5000);
};