代码之家  ›  专栏  ›  技术社区  ›  SBB

角度-如何消除通话中的振动?

  •  0
  • SBB  · 技术社区  · 8 年前

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

    问题是,如果我打字太快,它会打多个电话,有时会跳过我键入并使用的最后一个字符。

    我正在寻找一种方法来控制这种情况,并发现 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个网络呼叫。我想我应该只看一个?

    enter image description here

    0 回复  |  直到 8 年前