summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/assets/javascripts/lib/utils/poll.js14
1 files changed, 9 insertions, 5 deletions
diff --git a/app/assets/javascripts/lib/utils/poll.js b/app/assets/javascripts/lib/utils/poll.js
index ad0884a784d..c30a1fcb5da 100644
--- a/app/assets/javascripts/lib/utils/poll.js
+++ b/app/assets/javascripts/lib/utils/poll.js
@@ -36,6 +36,7 @@ export default class Poll {
this.options.data = options.data || {};
this.intervalHeader = 'POLL-INTERVAL';
+ this.timeoutID = null;
this.canPoll = true;
}
@@ -44,11 +45,8 @@ export default class Poll {
const pollInterval = headers[this.intervalHeader];
if (pollInterval > 0 && response.status === httpStatusCodes.OK && this.canPoll) {
- setTimeout(() => {
- // Stop can be called in the meanwhile, so let's check again.
- if (this.canPoll) {
- this.makeRequest();
- }
+ this.timeoutID = setTimeout(() => {
+ this.makeRequest();
}, pollInterval);
}
@@ -63,7 +61,13 @@ export default class Poll {
.catch(error => errorCallback(error));
}
+ /**
+ * Stops the polling recursive chain
+ * and guarantees if the timeout is already running it won't make another request by
+ * cancelling the previously established timeout.
+ */
stop() {
this.canPoll = false;
+ clearTimeout(this.timeoutID);
}
}