Counting keypresses per second with angular 2 Rxjs

后端 未结 1 788
不知归路
不知归路 2021-01-26 19:14
/**
 * Created by darius on 02/04/16.
 */
import { Component } from \'angular2/core\';
import { Observable } from \'rxjs/Rx\';


@Component({
  styles: [ require(\'../..         


        
相关标签:
1条回答
  • 2021-01-26 19:31

    I think that you could leverage the buffer operator. It allows to buffer events and sends them after an amount of time. You could then map this list of events to its length.

    var source = Observable.fromEvent(document.body, 'keyup');
    
    var obs = source
         .bufferTime(1000).map((clickBuffer) => {
           return clickBuffer.length;
         });
    
    obs.subscribe((num) => {
      // Get the number of pressed keys per second
    });
    

    See these links:

    • https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/buffer.md
    • How to create a RxJS buffer that groups elements in NodeJS but that does not rely on forever running interval?
    0 讨论(0)
提交回复
热议问题