Reno Reno - 6 months ago 21
Javascript Question

How to abort core-ajax in Polymer

say I have a

core-ajax
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
handler
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.

Answer

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. }

Docs: http://www.polymer-project.org/docs/polymer/polymer.html#job

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.

Comments