Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何使用rxjs limit click button 2s?;_Angular_Rxjs - Fatal编程技术网

Angular 如何使用rxjs limit click button 2s?;

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

对于每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) = "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>
你好