1

Following the documentation of rxjs and different guide pages, does not solve my problem that debounceTime does not work.

function getValue() {
  return new rxjs.Observable(sub => {
    let counter = 0;
    setInterval(() => {
      counter++;
      sub.next(counter);
    }, 100);
  });
}


// Removing debounceTime works or set it to a value < 100.
getValue().pipe(rxjs.operators.debounceTime(1000)).subscribe(data => {
  console.log(data);
});

https://jsfiddle.net/5bp1cwho/7/

I expected that the value comes every seconds instead of 100 ms.

1 Answer 1

4

debounceTime

signature: debounceTime(dueTime: number, scheduler: Scheduler): Observable

Discard emitted values that take less than the specified time between output

All your emitted items are spaced by less than 1000ms, so they are discarded.

Note : by default, the first item is not emitted directly.

If you want to get only the last operation auditTime is the operator you are searching for.

auditTime

Ignores source values for duration milliseconds, then emits the most recent value from the source Observable, then repeats this process.

function getValue() {
  return rxjs.interval(100);
}


// keep the last element after 1000ms 
getValue().pipe(rxjs.operators.auditTime(1000)).subscribe(data => {
  console.log(data);
});

If you want to do a specific processing of all the elements received within the 1000ms, you can use bufferTime.

bufferTime

signature: bufferTime(bufferTimeSpan: number, bufferCreationInterval: number, scheduler: Scheduler): Observable Collect emitted values until provided time has passed, emit as array.

function getValue() {
  return rxjs.interval(100);
}

getValue().pipe(
  rxjs.operators.bufferTime(1000), 
  rxjs.operators.map(itemsList => Math.max(itemsList))
)
.subscribe(data => {
  console.log(data);
});
6
  • 1
    Thanks. I want the last value every 1000 ms. Not all values within 1000 ms. Not bufferTime. e.g. 1, 2, 3, 4, 5, 6 (each all 100ms) to 10, 20, 30 (each all 1 sec).
    – Domske
    Commented Jul 16, 2018 at 10:42
  • "Yes" thanks. It there no official way with a single operator? In my opinion debounce should "debounce". Discard multiple signals within the time, but emitting the last value.
    – Domske
    Commented Jul 16, 2018 at 10:47
  • 2
    ok auditTime works more like expected (for me) debounceTime :D thanks.
    – Domske
    Commented Jul 16, 2018 at 10:52
  • 1
    Late comment, but I'm again confronted with rxj's time operations and I found another solution throttleTime. It's like auditTime but emits the first value.
    – Domske
    Commented Jan 11, 2019 at 14:57
  • 1
    In addition sampleTime is also great for such cases. Maybe the first solution. Don't forget to initialize your subject with a value. Some operators requires it to trigger on first receive. In my case sampleTime is very helpful to "debounce" (control refresh) incoming data. I hope I could help.
    – Domske
    Commented Apr 22, 2020 at 14:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.