diff options
author | Jo-Philipp Wich <jo@mein.io> | 2018-04-26 09:39:22 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2018-04-26 09:40:17 +0200 |
commit | aa6c97154e83c388f89274c4eb3f4c518fb8aac4 (patch) | |
tree | 3aa98eea4ea1b69d30cbe87cf7aaf3dc4f491f53 /modules/luci-base/htdocs/luci-static/resources | |
parent | 65479e3236afecfc8efa51fbcd831927e1ce8c56 (diff) |
luci-base: extend xhr.js
Add timeout options to get() and post() and introduce XHR.stop() to support
stopping a poll operation.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/htdocs/luci-static/resources')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/xhr.js | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/xhr.js b/modules/luci-base/htdocs/luci-static/resources/xhr.js index 3385f8f230..91dcf3fefe 100644 --- a/modules/luci-base/htdocs/luci-static/resources/xhr.js +++ b/modules/luci-base/htdocs/luci-static/resources/xhr.js @@ -39,7 +39,7 @@ XHR = function() this._xmlHttp.abort(); } - this.get = function(url,data,callback) + this.get = function(url,data,callback,timeout) { this.reinit(); @@ -54,6 +54,9 @@ XHR = function() else url += '?' + code; + if (!isNaN(timeout)) + xhr.timeout = timeout; + xhr.open('GET', url, true); xhr.onreadystatechange = function() @@ -76,7 +79,7 @@ XHR = function() xhr.send(null); } - this.post = function(url,data,callback) + this.post = function(url,data,callback,timeout) { this.reinit(); @@ -89,6 +92,9 @@ XHR = function() callback(xhr); } + if (!isNaN(timeout)) + xhr.timeout = timeout; + xhr.open('POST', url, true); xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.send(code); @@ -168,7 +174,7 @@ XHR.get = function(url, data, callback) (new XHR()).get(url, data, callback); } -XHR.poll = function(interval, url, data, callback) +XHR.poll = function(interval, url, data, callback, post) { if (isNaN(interval) || interval < 1) interval = 5; @@ -181,22 +187,38 @@ XHR.poll = function(interval, url, data, callback) 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); + e.xhr[post ? 'post' : 'get'](e.url, e.data, e.callback, e.interval * 1000 - 5); } XHR._t++; }; } - XHR._q.push({ + var e = { interval: interval, callback: callback, url: url, data: data, xhr: new XHR() - }); + }; + XHR._q.push(e); XHR.run(); + + return e; +} + +XHR.stop = function(e) +{ + for (var i = 0; XHR._q && XHR._q[i]; i++) { + if (XHR._q[i] === e) { + e.xhr.cancel(); + XHR._q.splice(i, 1); + return true; + } + } + + return false; } XHR.halt = function() |