简体   繁体   English

如何将设置间隔添加到轮询脚本

[英]How to add set intervals to polling script

We have an ajax system setup to check for unread messages during different events using a chrome browser: 我们有一个ajax系统设置,可以使用Chrome浏览器在不同事件期间检查未读消息:

var JHR = function ( method, url, data, fun ) {
  var xhr = new XMLHttpRequest();
  xhr.responseJSON = null;

  // xhr.open( 'POST', url );
  xhr.open(method, url, true);
  xhr.setRequestHeader("Authorization", "Basic " + data.userAuth);
  xhr.setRequestHeader( 'Content-Type', 'application/json' );

  xhr.addEventListener( 'load',  function () {
    //my old man once told me "Zirak, sometimes, you must face the dangers
    // of life head on". he then threw me in a shark tank. but I'm sure it
    // applies here too.
    //...I was only 7
    xhr.responseJSON = JSON.parse( xhr.responseText );

    if (fun) fun( xhr.responseJSON, xhr );
  });

  xhr.send( JSON.stringify(data) );

  return xhr;
};


var pollInterval = 1*60*60; // 60 min
var timerId;

function startRequest() {
    updateBadge();
    timerId = window.setTimeout(startRequest, pollInterval);
}
function stopRequest() {
    window.clearTimeout(timerId);
}

function getUnreadCount(onSuccess, onError) {
  chrome.storage.sync.get("userAuth", function(data) {
    var response = JHR("GET", "https://api.rss.com/v2/unread_entries.json", {userAuth: data.userAuth}); 
    console.log(response);
  }); 
}

function updateBadge() {
    // Callbacks for onSuccess, and onError
    getUnreadCount(function(data) {
        chrome.browserAction.setBadgeText({text:data.unreadItems});
    }, function(data){
        chrome.browserAction.setBadgeText({text:'error'});
    });
}

We would like to set a variable for an interval (every 5 minutes for example) and only send the ajax call out every 5 minutes or whatever the variable might be set to. 我们想为变量设置一个间隔(例如每5分钟),并且仅每5分钟发送一次ajax调用,或者将变量设置为任何值。

Can someone help us determine where to add this behavior? 有人可以帮助我们确定在何处添加此行为吗? I thought the pollInterval would handle it but it doesn't. 我以为pollInterval会处理它,但不会。

I would add a onreadystatechange function to your xhr object to just call the function again upon completion. 我会向您的xhr对象添加一个onreadystatechange函数,以在完成后再次调用该函数。 Inside your JHR declaration: 在您的JHR声明中:

xhr.onreadystatechange = function() {
  if(xhr.readyState == 4) {
    timerId && clearTimeout(timerId);
    timerId = setTimeout(startRequest, pollInterval);
  }
}

If you were to use setInterval instead you should check the previous xhr object for completion (via its readyState property) before firing another one. 如果要使用setInterval,则应在触发另一个xhr对象之前(通过其readyState属性)检查其是否完成。

Try changing your code to use clearInterval : 尝试更改您的代码以使用clearInterval

var pollInterval = 1*60*60; // 60 min
var timerId;

function startRequest() {
    updateBadge();
    timerId = window.setInterval(startRequest, pollInterval);
}

function stopRequest() {
    window.clearInterval(timerId);
}

More about setInterval : 有关setInterval更多信息:

http://www.w3schools.com/jsref/met_win_clearinterval.asp http://www.w3schools.com/jsref/met_win_clearinterval.asp

https://developer.mozilla.org/en-US/docs/Web/API/Window.clearInterval https://developer.mozilla.org/en-US/docs/Web/API/Window.clearInterval

setInterval and how to use clearInterval setInterval以及如何使用clearInterval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM