Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. // Returns a function, that, as long as it continues to be invoked, will not
  2. // be triggered. The function will be called after it stops being called for
  3. // N milliseconds. If `immediate` is passed, trigger the function on the
  4. // leading edge, instead of the trailing.
  5. function debounce(func, wait, immediate) {
  6. var timeout;
  7. return function() {
  8. var context = this, args = arguments;
  9. var later = function() {
  10. timeout = null;
  11. if (!immediate) func.apply(context, args);
  12. };
  13. var callNow = immediate && !timeout;
  14. clearTimeout(timeout);
  15. timeout = setTimeout(later, wait);
  16. if (callNow) func.apply(context, args);
  17. };
  18. };
  19.  
  20.  
  21. var myEfficientFn = debounce(function() {
  22. // All the taxing stuff you do
  23. }, 250);
  24.  
  25. window.addEventListener('resize', myEfficientFn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement