[英]Polymer iron-ajax interval calls
I have a simple Polymer frontend app. 我有一个简单的Polymer前端应用程序。 In it, I have simple ajax to get data from the backend: 在其中,我有一个简单的ajax从后端获取数据:
<iron-ajax id="getAjax"
auto
url="http://localhost:14039/InvoiceService/envelop/{{envelopId}}/invoices"
handle-as="json"
last-response="{{invoices}}">
The ajax gets called at startup and works. Ajax在启动时被调用并工作。 Whenever I POST using other iron-ajaxes I call this.$.getAjax.generateRequest();
每当我使用其他铁ajax进行发布时,我都将其命名this.$.getAjax.generateRequest();
and it works. 而且有效。
Question is, how can I call this function using some sort of timer. 问题是,如何使用某种计时器调用此函数。 The idea here is to "refresh" the page using iron-ajax. 这里的想法是使用Iron-ajax“刷新”页面。 I saw some answers on how to do it on JQuery , but I get confused with Polymers properties vs functions vs internal functions vs this.$ etc. 我在JQuery上看到了一些有关如何执行此操作的答案,但是我对Polymers属性vs函数vs内部函数vs this。$等感到困惑。
You would use Polymer's async()
method, as described in the docs : 您将使用Polymer的async()
方法,如docs中所述:
async(method, [wait])
.async(method, [wait])
。 Callsmethod
asynchronously. 异步调用method
。 If no wait time is specified, runs tasks with microtask timing (after the current method finishes, but before the next event from the event queue is processed). 如果未指定等待时间,则以微任务计时运行任务(在当前方法完成之后,但在处理事件队列中的下一个事件之前)。 Returns a handle that can be used to cancel the task. 返回可用于取消任务的句柄。
cancelAsync(handle)
.cancelAsync(handle)
。 Cancels the identified async task. 取消已标识的异步任务。
The following example defines _updateData()
that asynchronously re-sends the AJAX request after 2 seconds. 以下示例定义了_updateData()
,该方法在2秒后异步重新发送AJAX请求。 This could be called inside an on-response
handler of <iron-ajax>
so that the request is resent 2 seconds after every response. 可以在<iron-ajax>
on-response
处理器中调用此函数,以便在每次响应后2秒重新发送请求。
Polymer({
is: 'x-foo',
_updateData: function() {
this.async(function() {
this.$.ajax.generateRequest();
}, 2000);
},
_onResponse: function() {
this._updateData();
}
});
Here's a working demo: 这是一个工作示例:
<head> <base href="https://polygit.org/polymer+1.11.1/components/"> <script src="webcomponentsjs/webcomponents-lite.js"></script> <link rel="import" href="polymer/polymer.html"> <link rel="import" href="iron-ajax/iron-ajax.html"> </head> <body> <x-foo></x-foo> <dom-module id="x-foo"> <template> <iron-ajax id="ajax" auto url="//jsonplaceholder.typicode.com/users/" last-response="{{data}}" on-response="_onResponse" handleAs="json"> </iron-ajax> <table> <template is="dom-repeat" items="[[data]]"> <tr> <td>[[item.id]]</td> <td>[[item.name]]</td> <td>[[item.email]]</td> </tr> </template> </table> <div> <sup>See console log</sup> </div> </template> <script> HTMLImports.whenReady(function() { Polymer({ is: 'x-foo', _updateData: function() { this.async(function() { this.$.ajax.generateRequest(); }, 2000); }, _onResponse: function() { console.log('received response'); this._updateData(); } }); }); </script> </dom-module> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.