themes/base: xhr.js: clean code style, implement XHR.get() and XHR.poll() convenience functions
This commit is contained in:
parent
2829139f35
commit
b2b3b181d8
1 changed files with 82 additions and 41 deletions
|
@ -19,6 +19,9 @@ XHR = function()
|
|||
}
|
||||
|
||||
this.busy = function() {
|
||||
if (!this._xmlHttp)
|
||||
return false;
|
||||
|
||||
switch (this._xmlHttp.readyState)
|
||||
{
|
||||
case 1:
|
||||
|
@ -161,3 +164,41 @@ XHR = function()
|
|||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
XHR.get = function(url, data, callback)
|
||||
{
|
||||
(new XHR()).get(url, data, callback);
|
||||
}
|
||||
|
||||
XHR.poll = function(interval, url, data, callback)
|
||||
{
|
||||
if (isNaN(interval) || interval <= 1)
|
||||
interval = 5;
|
||||
|
||||
if (!XHR._q)
|
||||
{
|
||||
XHR._t = 0;
|
||||
XHR._q = [ ];
|
||||
}
|
||||
|
||||
XHR._q.push({
|
||||
interval: interval,
|
||||
callback: callback,
|
||||
url: url,
|
||||
data: data,
|
||||
xhr: new XHR()
|
||||
});
|
||||
|
||||
if (!XHR._i)
|
||||
{
|
||||
XHR._i = window.setInterval(function() {
|
||||
for (var i = 0, e = XHR._q[0]; i < XHR._q.length; e = XHR._q[++i])
|
||||
{
|
||||
if (!(XHR._t % e.interval) && !e.xhr.busy())
|
||||
e.xhr.get(e.url, e.data, e.callback);
|
||||
}
|
||||
|
||||
XHR._t++;
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue