mdrndv mdrndv - 6 months ago 14
Ajax Question

Request URL until required data arrives

What is the best way to periodically request URL and cancel requests when required data is present in the response in plain Javascript?

Say, I am requesting

URI that returns JSON and want to cancel requests when
attribute in response is equal than some



Combine setInterval() with your web service call and cancel the interval once you got the expected response via clearInterval().

MDN about setInterval():

Repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. Returns an intervalID.