From 70e59559ce65daf663077421243c45e2bc0ccf49 Mon Sep 17 00:00:00 2001 From: Filipa Lacerda <filipa@gitlab.com> Date: Thu, 23 Mar 2017 16:12:43 +0000 Subject: [PATCH] Clears timeout --- app/assets/javascripts/lib/utils/poll.js | 14 +++++++++----- 1 file 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); } } -- GitLab