Skip to content

Instantly share code, notes, and snippets.

@grumd
Last active August 24, 2016 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grumd/0566e1054c403713ed70f1418ffbe373 to your computer and use it in GitHub Desktop.
Save grumd/0566e1054c403713ed70f1418ffbe373 to your computer and use it in GitHub Desktop.
Debounce vs Throttle

debounce и throttle это встроенные в lodash функции, позволяющие ограничивать частоту вызова другой функции.

$("#inputbox1").on('onchange', _.debounce(function() {
  // Action
}, 1000));

Debounce - функция вызовется только спустя указанное количество мс, если эта же функция не была вызвана за этот период. Использование, например, в случае текстового поля: функция вызовется, когда пользователь закончил печатать.

$("body").on('scroll', _.throttle(function() {
  // Action  
}, 100));

Throttle - функция вызовется сразу, но последующие вызовы в указанный период будут проигнорированны.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment