Angular 如何使用rxjs limit click button 2s?;
对于每2秒钟单击一次按钮,我应该怎么做?Rxjs有很多操作符,比如throttleTime。但是语法是:Angular 如何使用rxjs limit click button 2s?;,angular,rxjs,Angular,Rxjs,对于每2秒钟单击一次按钮,我应该怎么做?Rxjs有很多操作符,比如throttleTime。但是语法是: Rx.Observable.fromEvent(button, 'click') .throttleTime(1000) .scan(count => count + 1, 0) .subscribe(count => console.log(`Clicked ${count} times`)); // html <button (click) = "cli
Rx.Observable.fromEvent(button, 'click')
.throttleTime(1000)
.scan(count => count + 1, 0)
.subscribe(count => console.log(`Clicked ${count} times`));
// html
<button (click) = "clickMe()">
// ts
clickMe(){
alert('yes!!!');
}
我使用angular和Rxjs。语法是:
Rx.Observable.fromEvent(button, 'click')
.throttleTime(1000)
.scan(count => count + 1, 0)
.subscribe(count => console.log(`Clicked ${count} times`));
// html
<button (click) = "clickMe()">
// ts
clickMe(){
alert('yes!!!');
}
//html
//ts
点击我(){
警惕(“是!!!”);
}
这里合适的操作员可能是
debounceTime(2000)
当发生多次单击时,如果出现中断(在这种情况下为2秒),则最后一次单击将被激活。这是您描述的用例中所需的行为
throttleTime()
只是在指定的时间内暂停单击。以角度方式,通过@ViewChild
装饰器获取按钮的引用。在ngAfterViewInit
lifecyclehook中:
@ViewChild('btn', { read: ElementRef }) button: ElementRef;
ngAfterViewInit() {
...
Observable.fromEvent(this.button.nativeElement, 'click')
.throttleTime(2000) // change from one second to two
.scan(count => count + 1, 0)
.subscribe(count => console.log(`Clicked ${count} times`));
}
模板:
<button #btn>Hello</button>
你好