Reputation: 1883
I am trying to find a way to simulate code below in RxJava or RxJs.
var timerRef;
run()
run()
function run(){
if(timerRef){
clearTimeout(timerRef);
}
timerRef = setTimeout(function(){alert("hello")}, 1000);
}
Basically, once I got the emitted message, I want to wait for a while and make sure there is no coming message, if there is, cancel the current one and repeat the waiting for the new one, if not pass it to subscribe method.
I know, the current message should not know about previous or next emitted to the pipeline but how can I achieve it?
Upvotes: 0
Views: 22
Reputation: 11345
Sounds like debounceTime
operator is your choice. Here i replace run() with a document click event
import { fromEvent } from 'rxjs';
import { debounceTime, map } from 'rxjs/operators';
const source = fromEvent(document, 'click')
source.pipe(
debounceTime(3000)
)
.subscribe(console.log);
https://stackblitz.com/edit/typescript-fbyrsh
Upvotes: 1