Guest User

Untitled

a guest
Jun 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. /** @function
  2. * @name debounce
  3. * @description Utility method for debouncing the resize event
  4. * @param {function} func
  5. * @param {number} wait
  6. * @param {object} immediate
  7. * @example var myEfficientFn = debounce(function() { things to do }, 250);
  8. * @example window.addEventListener( 'resize', myEfficientFn );
  9. */
  10.  
  11. var debounce = function ( func, wait, immediate ) {
  12.  
  13. var timeout;
  14. return function() {
  15.  
  16. var context = this;
  17. var args = arguments;
  18.  
  19. var later = function() {
  20. timeout = null;
  21. if ( ! immediate ) {
  22. func.apply( context, args );
  23. }
  24. };
  25. var callNow = immediate && !timeout;
  26.  
  27. window.clearTimeout( timeout );
  28. timeout = window.setTimeout( later, wait );
  29.  
  30. if ( callNow ) {
  31. func.apply( context, args );
  32. }
  33.  
  34. };
  35. };
Add Comment
Please, Sign In to add comment