【问题标题】:I want to stop the second request that is sent by the iron-ajax even before the first request sent is completed即使在发送的第一个请求完成之前,我也想停止 Iron-ajax 发送的第二个请求
【发布时间】:2018-12-27 06:49:54
【问题描述】:

我有一个 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}}">

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

【问题讨论】:

  • 您可能想要使用所有浏览器都原生提供的 Fetch API:developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
  • @PascalL。我也试过 fetch 但我也得到了同样的回应。如果你修复的时间少于 2 分钟,它会很好。
  • debounce-duration 只能为自动发送的请求设置。
  • 超时发生在您设置的时间,但在此之前,将向服务器发送新请求。

标签: polymer polymer-3.x iron-ajax


【解决方案1】:

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

DEMO

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 等。

【讨论】:

  • HakanC。感谢您的解决方案。是的,debounce-duration 在这个场景中根本不需要,因为只有在自动而不是手动发送请求时才使用它。我会按照您的建议尝试并告诉您结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 2015-08-25
  • 1970-01-01
相关资源
最近更新 更多