From: Mike Gelfand Date: Sun, 15 Jan 2017 08:16:50 +0000 (+0300) Subject: Replace setInterval() with setTimeout() for web UI refresh (patch by WGH) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a3654c65a59f4a386ca566dde2d8c713cf0136b1;p=transmission Replace setInterval() with setTimeout() for web UI refresh (patch by WGH) Fixes: TRAC-6031 --- diff --git a/web/javascript/inspector.js b/web/javascript/inspector.js index 0ad78872b..14164dfcd 100644 --- a/web/javascript/inspector.js +++ b/web/javascript/inspector.js @@ -23,7 +23,7 @@ function Inspector(controller) { return false; }, - refreshTorrents = function () { + refreshTorrents = function (callback) { var fields, ids = $.map(data.torrents.slice(0), function (t) { return t.getId(); @@ -36,7 +36,7 @@ function Inspector(controller) { $.merge(fields, Torrent.Fields.InfoExtra); } - data.controller.updateTorrents(ids, fields); + data.controller.updateTorrents(ids, fields, callback); } }, @@ -851,7 +851,8 @@ function Inspector(controller) { ****/ this.setTorrents = function (torrents) { - var d = data; + var d = data, + that = this; // update the inspector when a selected torrent's data changes. $(d.torrents).unbind('dataChanged.inspector'); @@ -859,8 +860,17 @@ function Inspector(controller) { d.torrents = torrents; // periodically ask for updates to the inspector's torrents - clearInterval(d.refreshInterval); - d.refreshInterval = setInterval($.proxy(refreshTorrents, this), 2000); + clearTimeout(d.refreshTimeout); + + function callback() { + refreshTorrents(rescheduleTimeout); + } + + function rescheduleTimeout() { + d.refreshTimeout = setTimeout(callback, 2000); + } + + rescheduleTimeout(); refreshTorrents(); // refresh the inspector's UI diff --git a/web/javascript/transmission.js b/web/javascript/transmission.js index 748cebf38..6502182a5 100644 --- a/web/javascript/transmission.js +++ b/web/javascript/transmission.js @@ -113,12 +113,16 @@ Transmission.prototype = { this.updateButtonsSoon(); }, - loadDaemonPrefs: function (async) { + loadDaemonPrefs: function (async, callback) { this.remote.loadDaemonPrefs(function (data) { var o = data['arguments']; Prefs.getClutchPrefs(o); this.updateGuiFromSession(o); this.sessionProperties = o; + + if (callback) { + callback(); + } }, this, async); }, @@ -632,14 +636,23 @@ Transmission.prototype = { // turn the periodic ajax session refresh on & off togglePeriodicSessionRefresh: function (enabled) { - clearInterval(this.sessionInterval); - delete this.sessionInterval; - if (enabled) { - var callback = $.proxy(this.loadDaemonPrefs, this); - var msec = 8000; + var that = this, + msec = 8000; - this.sessionInterval = setInterval(callback, msec); - }; + function callback() { + that.loadDaemonPrefs(undefined, rescheduleTimeout); + } + + function rescheduleTimeout() { + that.sessionTimeout = setTimeout(callback, msec); + } + + clearTimeout(this.sessionTimeout); + delete this.sessionTimeout; + + if (enabled) { + rescheduleTimeout(); + } }, toggleTurtleClicked: function () { @@ -829,8 +842,18 @@ Transmission.prototype = { } }, - updateTorrents: function (ids, fields) { - this.remote.updateTorrents(ids, fields, this.updateFromTorrentGet, this); + updateTorrents: function (ids, fields, callback) { + var that = this; + + function f(updates, removedIds) { + if (callback) { + callback(); + } + + that.updateFromTorrentGet(updates, removedIds); + } + + this.remote.updateTorrents(ids, fields, f); }, refreshTorrents: function () { @@ -1676,20 +1699,32 @@ Transmission.prototype = { // turn the periodic ajax stats refresh on & off togglePeriodicStatsRefresh: function (enabled) { - clearInterval(this.statsInterval); - delete this.statsInterval; + var that = this, + msec = 5000; - if (enabled) { - var callback = $.proxy(this.loadDaemonStats, this); - var msec = 5000; + function callback() { + that.loadDaemonStats(undefined, rescheduleTimeout); + } - this.statsInterval = setInterval(callback, msec); - }; + function rescheduleTimeout() { + that.statsTimeout = setTimeout(callback, msec); + } + + clearTimeout(this.statsTimeout); + delete this.statsTimeout; + + if (enabled) { + rescheduleTimeout(); + } }, - loadDaemonStats: function (async) { + loadDaemonStats: function (async, callback) { this.remote.loadDaemonStats(function (data) { this.updateStats(data['arguments']); + + if (callback) { + callback(); + } }, this, async); },