关于rxjs debounceTime的困惑

问题描述 投票:1回答:1

我有这个方法,在滚动表时会被多次调用:

setViewportRange(firstRow: number, lastRow: number): void {
  this.getViewport(firstRow, lastRow).subscribe(message => {
    console.log(message);
  });
}

我没有对setViewportRange方法的调用的控制,但是我需要对其进行反跳操作。所以我用lodash的debounce函数创建了这个debouncedGetViewport方法:

setViewportRange(firstRow: number, lastRow: number): void {
  this.debouncedGetViewport(firstRow, lastRow);
}

debouncedGetViewport = debounce((firstRow, lastRow) => {
  return this.getViewport(firstRow, lastRow).subscribe(message => {
    console.log(message);
  });
}, 1000);

有效!但是一位同事问我为什么不使用RxJ的反跳功能。所以我试图用RxJs实现它,但是我无法使它工作。无论传递哪个值,debounceTime均无效!您能帮我了解为什么这不起作用吗?我想我误会了。

setViewportRange(firstRow: number, lastRow: number): void {
  this.debouncedGetViewport(firstRow, lastRow);
}

debouncedGetViewport = (firstRow, lastRow) => {
  return this.getViewport(firstRow, lastRow)
    .pipe(debounceTime(1000))
    .subscribe(message => {
      console.log(message);
    });
};

谢谢!

typescript rxjs lodash debounce
1个回答
2
投票

首先!

© www.soinside.com 2019 - 2024. All rights reserved.