Ajax长轮询如何检测它是否';它目前正在运行

Ajax长轮询如何检测它是否';它目前正在运行,ajax,long-polling,Ajax,Long Polling,我们正在使用轮询功能,但有4个地方需要确保轮询正在运行。我们如何确定当前轮询实例是否正在运行,以便不创建另一个实例,并进行重叠轮询 function longPoll(){ // do the request chrome.storage.local.get("userAuth", function(data) { if(data.hasOwnProperty('userAuth')){ if(!localStorage.disab

我们正在使用轮询功能,但有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);
    };

您可以设置一个布尔变量来跟踪轮询当前是否正在运行。比如说:

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