首页 > 解决方案 > Angular - 如何消除电话抖动?

问题描述

我有一个页面,用户可以在其中输入一个值到一个字段中,它执行搜索并将其返回给用户。

问题是,如果我输入得太快,它会进行多次调用,有时会跳过我输入的最后一个字符并使用它之前的调用。

我正在寻找一种方法来限制它并发现debounce.

我觉得我没有正确使用它,因为它似乎对我正在做的事情没有影响。

// Component
sub: Subscription;

this.sub = this._empSearchService.employeeSearchData$
    .debounceTime(1000)
    .subscribe(
        results => {

            if (results) {
            ...
            }
        }
    );

...

// On Key Up Event
this._empSearchService.searchMultipleEmployees(data)
.then((data: any) => {
    this._empSearchService.updateSearchedEmployees(data);
});



// Service

private employeeSearchSub = new BehaviorSubject<any>(null);
employeeSearchData$ = this.employeeSearchSub.asObservable();

updateSearchedEmployees(obj) {
    this.employeeSearchSub.next(obj);
};

对此有什么想法吗?

我担心的是,当我在 1 秒内输入 4 个字符时,我会看到 4 个网络调用。我想我应该只看到一个?

在此处输入图像描述

标签: angulartypescriptrxjs

解决方案


推荐阅读