1

我试图getResponse在此 Web 组件加载时运行一次,然后在每次过程属性更改时运行。

然而,当试图运行这个debounce函数时,它被调用了 4 次,并getResponse在延迟后运行,但是 4 次而不是 1 次。

static get properties() {
    return {
      procedure: {
        type: String,
        observer: 'getResponse'
      },
      timeout: {
        type: Number,
        value: null
      }

    }
  }

  ready() {
    super.ready();
    this.debounce();
  }

  debounce() {
    console.log('debounce');
    this.procedure = 'getInventoryActive';
    clearTimeout(this.timeout) // Clear the timeout if it has already been set.
                               // This will prevent the previous task from executing if it has been less than <MILLISECONDS>
    let that = this;           // Make a new timeout set to go off in 800ms
    this.timeout = setTimeout( () => {
      that.getResponse();
    }, 8000);
  }

  getResponse() {
    // do something;
  }

}

我怎样才能实现这种行为?

PS还在ready函数中尝试了这种去抖方法,它仍然调用getResponse 4次......(https://codepen.io/tony19/pen/vxZVwx

debounce() {
  this.procedure = 'name';
  this._debouncer = Polymer.Debouncer.debounce(this._debouncer, Polymer.Async.timeOut.after(8000), () => {
    this.getResponse();
  });
}
4

0 回答 0