Reno Reno - 5 months ago 7x
Javascript Question

How to abort core-ajax in Polymer

say I have a

element configured as such:

<core-ajax url='/api/endpoint' response='{{handler}}'></core-ajax>

say I happen to hit the API at a higher frequency than inverse response time.

How do I ensure that
only gets triggered by the last call to the api, regardless of return order? Basically when I make a new call I want to ignore all previous calls that haven't returned yet.


One way to do this is with job() to push off work:

this.responseChanged = function() { this.job('job1', function() { // first arg is the name of the "job" ... }, 100); // delay callback 100ms. If responseChanged() is called again, it'll be another 100ms later...etc. }


You can call it repeatedly before the timeout but it only results in a single side-effect. In other words, if responseChanged() is immediately executed 250ms later, its callback won't be executed until 750ms.