Can someone explain the “debounce” function in Javascript

后端 未结 8 785
旧时难觅i
旧时难觅i 2020-11-22 02:57

I am interested in the \"debouncing\" function in javascript, written here : http://davidwalsh.name/javascript-debounce-function

Unfortunately the code is not explai

8条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 03:25

    Debounced functions do not execute when invoked, they wait for a pause of invocations over a configurable duration before executing; each new invocation restarts the timer.

    Throttled functions execute and then wait a configurable duration before being eligible to fire again.

    Debounce is great for keypress events; when the user starts typing and then pauses you submit all the key presses as a single event, thus cutting down on the handling invocations.

    Throttle is great for realtime endpoints that you only want to allow the user to invoke once per a set period of time.

    Check out Underscore.js for their implementations too.

提交回复
热议问题