停止打字时如何触发呼叫

  • 本文关键字:何触发 呼叫 javascript
  • 更新时间 :
  • 英文 :

<input autocomplete="off" [config]="config2"   [items]="items2" (inputChangedEvent)="onInputChangedEvent($event)"   (selectEvent)="onSelect($event)">`enter code here`

onInputChangedEvent(val: string) {
        this.changeEvent.emit(val);
        this.inputChanged = val;
        if (this.timer) {
            clearTimeout(this.timer);
        }
        // trigger the search action after 400 millis
        this.timer = setTimeout(this.searchFunction(val), 200);
}

我正在使用 inputchangedEvent ,我们如何延迟事件

您不能将用参数的函数传递给setTimeout(),您需要创建另一个函数,其中在此函数中:

var _this = this;
setTimeout(function () {
    _this.searchFunction(val);
}, 200);

通过将函数直接传递给SettiMeout,JavaScript执行该功能并将返回值用作回调。因此,您的searchFunction每次都执行。

您是否在没有400msec的键入时才触发操作?

如果是这样,通常的方法是" Deadman's Switch"(我相信从火车上的旧安全设备借用的名称)。

基本想法是,每次看到击键时,都会杀死现有的计时器(如果有)并开始使用400msec的计时器。

那么,如果计时器最终触发,您知道400msec已通过击键而通过,您可以执行操作。

最新更新