What does RxJS.Observable debounce do?

后端 未结 3 501
说谎
说谎 2020-12-10 11:32

Can anybody explain in plain English what RxJS Observable debounce function does?

I imagine it emits an event once in a while depending on the parameters, but my cod

3条回答
  •  误落风尘
    2020-12-10 12:05

    Long story short: debounce waits for X time that the stream isn't emitting any new value, then let the latest value pass.

    Long story: Once a value is emitted, debounce will pause its emission for X time to see if another value is emitted, in fact blocking the stream during this time. If a new value is emitted during the debounce time then the timer is restarted and debounce waits again for the full time. If its timer expires without any new value being emitted, it let the latest value pass.

    Let's say that you want to add autocomplete to an input box. If the user insert "a" you may want to show him the choices "acorn, alaska", but if the user right after press "l" you would propose just "alaska". In this case it's better to wait for the user to stop pressing the keyboards to avoid doing unnecessary work. debounce it's the right tool here: it waits for X time the the stream isn't emitting any new value

提交回复
热议问题