diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2011-09-30 17:44:11 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2011-09-30 17:44:11 +0000 |
commit | 3b2d4ab1834693d802e0aad7037832da502eae3a (patch) | |
tree | 18a388bfa237ccf32bac3ac754660439a3730897 /themes/base/htdocs/luci-static | |
parent | 7aca8f2381c9821bf9b208db45de04bad9f21311 (diff) |
themes/base: xhr.js: implement running(), run() and halt() to check, start and stop the AJAX poll queue
Diffstat (limited to 'themes/base/htdocs/luci-static')
-rw-r--r-- | themes/base/htdocs/luci-static/resources/xhr.js | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/themes/base/htdocs/luci-static/resources/xhr.js b/themes/base/htdocs/luci-static/resources/xhr.js index 453c2ba4e8..701c12ac19 100644 --- a/themes/base/htdocs/luci-static/resources/xhr.js +++ b/themes/base/htdocs/luci-static/resources/xhr.js @@ -172,13 +172,22 @@ XHR.get = function(url, data, callback) XHR.poll = function(interval, url, data, callback) { - if (isNaN(interval) || interval <= 1) + if (isNaN(interval) || interval < 1) interval = 5; if (!XHR._q) { XHR._t = 0; XHR._q = [ ]; + XHR._r = 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++; + }; } XHR._q.push({ @@ -189,16 +198,44 @@ XHR.poll = function(interval, url, data, callback) xhr: new XHR() }); - if (!XHR._i) + XHR.run(); +} + +XHR.halt = function() +{ + 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); - } + /* show & set poll indicator */ + try { + document.getElementById('xhr_poll_status').style.display = ''; + document.getElementById('xhr_poll_status_on').style.display = 'none'; + document.getElementById('xhr_poll_status_off').style.display = ''; + } catch(e) { } + + window.clearInterval(XHR._i); + XHR._i = null; + } +} - XHR._t++; - }, 1000); +XHR.run = function() +{ + if (XHR._r && !XHR._i) + { + /* show & set poll indicator */ + try { + document.getElementById('xhr_poll_status').style.display = ''; + document.getElementById('xhr_poll_status_on').style.display = ''; + document.getElementById('xhr_poll_status_off').style.display = 'none'; + } catch(e) { } + + /* kick first round manually to prevent one second lag when setting up + * the poll interval */ + XHR._r(); + XHR._i = window.setInterval(XHR._r, 1000); } } + +XHR.running = function() +{ + return !!(XHR._r && XHR._i); +} |