Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. function throttle(delay, no_trailing, callback, debounce_mode) {
  2. var timeout_id,
  3. last_exec = 0;
  4.  
  5. if ( typeof no_trailing !== "boolean" ) {
  6. debounce_mode = callback;
  7. callback = no_trailing;
  8. no_trailing = undefined;
  9. }
  10.  
  11. function wrapper() {
  12. var that = this,
  13. elapsed = +new Date() - last_exec,
  14. args = arguments;
  15.  
  16. function exec() {
  17. last_exec = +new Date();
  18. callback.apply( that, args );
  19. }
  20.  
  21. function clear() { timeout_id = undefined; }
  22.  
  23. if ( debounce_mode && !timeout_id ) { exec(); }
  24.  
  25. timeout_id && clearTimeout( timeout_id );
  26.  
  27. if ( debounce_mode === undefined && elapsed > delay ) {
  28. exec();
  29. }
  30. else if ( no_trailing !== true ) {
  31. timeout_id = setTimeout( debounce_mode ? clear : exec, debounce_mode === undefined ? delay - elapsed : delay );
  32. }
  33. }
  34.  
  35. if ( $.guid ) {
  36. wrapper.guid = callback.guid = callback.guid || $.guid++;
  37. }
  38.  
  39. return wrapper;
  40. }
  41.  
  42. export { throttle };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement