JavaScript Polling Issue

Hi All,

I come to you in need of help. If anyone could help me out, I’d greatly appreciate it.

Problem:
When I click “End”, it doesn’t destroy (lack of a better word) the setTimeout function.
You’ll notice the json file is called, at some point 6 times :frowning:

Note:
I left a console.log in the code to show you the issue I speak of (so ensure you have your console open).

Requirements:
By default the json file should load once, then upon clicking “Start”, the json file needs to be called (every x secs) until “End” is clicked (in which case the polling will stop).

I just had a quick look at your example page and everything seemed to work just fine. Are you still having an issue?

If you’re talking about the number that’s returned by console.log(_this.setPolling), that’s the ID of the setTimeOut not the # of times it’s been called.

Hi,

Thanks for the quick response (that’s good to know). If you click “Start” & “End” a few times, you’ll see the number of requests (json) double, if not triple. Let me know if you can’t replicate the issue. Keep clicking a few times and you’ll see.

Thanks again for your expertise.

Ah, I see. It looks as if it’s starting the polling setTimeOut multiple times.

I think the most straightforward way to get around that is to check if _this.setPolling is defined.

In addition to this, you will want to delete the reference to the timeout once it’s been cleared, i.e.

window.clearTimeout(_this.setPolling);
delete _this.setPolling;

It’s now working:

Thanks for your help man :slight_smile:

Awesome.

No worries dude, glad to be able to help :slight_smile: