在 Angular 中,可以使用 RxJS 的 debounceTime() 操作符控制单击按钮的去抖时间。下面是一个示例:
import { Component } from '@angular/core'; import { Subject } from 'rxjs/Subject'; import { debounceTime } from 'rxjs/operators';
@Component({
selector: 'app-button',
template:
})
export class ButtonComponent {
click$ = new Subject();
constructor() { this.click$.pipe(debounceTime(500)).subscribe(() => { console.log('Button clicked!'); }); } }
在模板中创建一个点击事件流 click$,然后使用 debounceTime() 操作符为该事件流添加去抖时间。在这个例子中,我们把去抖时间设置为 500 毫秒。
在组件的构造函数中订阅 click$ 事件流,当按钮被单击时,就会触发该流。如果按钮在 500 毫秒内再次被单击,上一次的回调函数将被取消,并重新开始计时。
通过以上步骤,我们就可以实现 Angular 中单击按钮的去抖时间的设置。