简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM