在处理单个页面应用程序时,我目前正在为所有的调用构建一个JQuery,Ajax函数。对于一个典型的页面,我可能有3个Ajax调用。我的想法是,如果用户互联网出去,将这些Ajax调用保存在数组中,然后做一个计时器,继续检查用户是否有互联网。一旦他们有了互联网,就可以打电话了。因此,当用户离线时(除了对InternetOne的检查之外),并且一旦他们回到在线时,他们会做他们想做的事情,就不会运行任何电话。
所以我有了这个代码:
beforeSend : function($xhr)
{
self.ajaxPool.push($xhr);
if(!self.validateConnection())
{
console.log(self.ajaxPool);
}
}
所以我的问题是,当我得到连接、循环遍历我的数组到每个$xhr对象时,我可以调用它的函数来说,‘嘿,做你现在应该做的’?例如$xhr.complete()? I'我在池上做了一个控制台日志,以便在连接关闭时查看对象,但是它其中的所有函数都不像是能做到这一点的样子。
I been using this Offline JS for my RIA applications. It's really reliable for your offline scenarios
I would ditch the beforeSend entirely since you're using a pool anyways and do something like this...
//where you want to make your initial request somewhere in code
//do this INSTEAD of calling $.ajax(options)
ajaxPool.push(options);
//an interval to handle the queue
setInterval(function(){
//nothing in the pool so do nothing
if(ajaxPool.length < 1) return;
//not online so don't try to send
if(!validateConnection()) return;
//hey we're online lets make the ajax request
doAjax(ajaxPool.shift());
},200);
function doAjax(options){
//add call backs since you seem to be using a single call back
options.complete=function(data){ ... };
//do call
$.ajax(options);
}
You could make this more OOP and you could pause the interval when you know it's not being used, but I think this gets the basic concept across.