假设我有一个 core-ajax
元素配置如下:
<core-ajax url='/api/endpoint' response='{{handler}}'></core-ajax>
假设我碰巧以比逆响应时间更高的频率访问 API。
如何确保 handler
仅由最后一次调用 api 触发,无论返回顺序如何?基本上,当我调用新电话时,我想忽略之前所有尚未返回的电话。
最佳答案
实现此目的的一种方法是使用 job()
来推迟工作:
this.responseChanged = function() {
this.job('job1', function() {//第一个参数是“job”的名称
...
}, 100);//延迟回调100ms。如果再次调用 responseChanged()
,则会再过 100 毫秒……等等。
}
文档: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.
关于javascript - 如何在 Polymer 中中止 core-ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26039232/