yr6m2a
Last Updated: February 25, 2016
·
1.767K
· maciejsmolinski

JavaScript: Debounce callback's execution by `limit` milliseconds

In case you want execute event listener's callback after given amount of time (milliseconds), you may find LoDash' _.debounce function extremely useful.

Sample Usage:

1) Define Callback:

function windowResized() {
  console.log('window resized');
}

2) Set milliseconds limit:

var limit = 300; // 300 milliseconds

3) Bind Event Listener (callback):

window.addEventListener('resize', _.debounce(windowResized, limit), false);

That's it! Now slowly resize your browser's window. Callback shouldn't execute until you stop resizing for at least 300ms!