代码之家  ›  专栏  ›  技术社区  ›  Kuba Å imonovský

聚合物3.0中的去抖剂

  •  1
  • Kuba Å imonovský  · 技术社区  · 6 年前

    如何正确写聚合物3中的去抖器?

    documentation

    import {microTask} from '@polymer/polymer/lib/utils/async.js';
    import {Debouncer} from '@polymer/polymer/lib/utils/debounce.js';
    // ...
    
    _debounceWork() {
      this._debounceJob = Debouncer.debounce(this._debounceJob,
          microTask, () => this._doWork());
    }
    

    这很好,但我需要配置一些时间。例如,这就是我在聚合物1中所做的

      this.debounce("scroll",function() {
          this.$$("#scrollThreshold").clearTriggers();
      }.bind(this), 400);
    

    this._debouncer = Polymer.Debouncer.debounce(
        this._debouncer, // initially undefined
        Polymer.Async.timeOut.after(400),
        () => {
           // some code
        }
    );
    

    但我不知道如何在聚合物3中设置400毫秒的去抖动

    1 回复  |  直到 6 年前
        1
  •  5
  •   Hyyan Abo Fakher JM1    6 年前

    这个 async 模块具有 timeout 但是你需要导入它

    import {timeOut} from '@polymer/polymer/lib/utils/async.js';
    
    this._debouncer = Debouncer.debounce(
        this._debouncer, // initially undefined
        timeOut.after(400),
        () => {
           // some code
        }
    );
    
    推荐文章