首页 > 解决方案 > 即使在发送的第一个请求完成之前,我也想停止 Iron-ajax 发送的第二个请求

问题描述

我有一个 api 需要 4 分钟(大约)才能在我的聚合物应用程序中完成。当我在准确的 2 分钟发送请求时,即使在超时并且去抖动持续时间设置为 10 分钟后,也会发送另一个请求。我想停止发送第二个请求或等待第一个请求完成。有些人建议我使用 fetch 而不是 Iron-ajax。我需要修复 iron-ajax 本身。我可以为此提供解决方案吗?

代码在这里

<iron-ajax id="request"
       url={{request}}
       verbose
       handle-as="json"
       method="{{method}}"
       body="{{body}}"
       params="{{requestParams}}"
       on-error="onError"
       loading="{{loading}}"
       timeout=600000
       content-type="application/json"
       debounce-duration=600000
       last-error="{{lastError}}"
       last-response="{{lastResponse}}">

我希望这会得到解决。提前致谢

标签: polymerpolymer-3.xiron-ajax

解决方案


您可以设置一个布尔变量,以便能够调用this.$.request.generateRequest()类似:

演示

static get properties() {return {
    _isRequest: {
         type: Boolean,
         value: true}}}

static get observers(){ return ['_checkLastResponse(lastResponse)'] }

__checkLastResponse(r) {
        // we have received a response from iron-ajax so allow for next call
        if (r) this._isRequest = true;}

// As you provided above code, you call ajax manually. 
_ajax_call() {
      if (this._isRequest) {
              this.$.request.generateRequest();
              this._isRequest = false; }
}

static get template() { return `
   <paper-button disabled="[[!_isReruest]]" on-tap="ajax_call">AJAX CALL</paper-button>
   .......
   `;}

因此,您可以删除不必要的属性等debounce-duration=600000


推荐阅读