Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. function debounce(fn, debounceTime) {
  2. if (typeof fn !== "function") {
  3. throw "debounce arg is not a function";
  4. }
  5.  
  6. var time = debounceTime || 300;
  7. var timeout;
  8.  
  9. return function() {
  10. if (timeout) {
  11. clearTimeout(timeout);
  12. timeout = undefined;
  13. }
  14.  
  15. timeout = setTimeout(fn, time);
  16. };
  17. }
  18.  
  19. function throttle(fn, throttleTime) {
  20. if (typeof fn !== "function") {
  21. throw "throttle arg is not a function";
  22. }
  23.  
  24. var time = throttleTime || 300;
  25. var scheduled = -1; // initial = -1, set - 1, not set = 0
  26.  
  27. return function() {
  28. if (scheduled === 1) {
  29. return;
  30. }
  31.  
  32. setTimeout(function() {
  33. fn();
  34. scheduled = 0;
  35. }, time);
  36.  
  37. scheduled = 1;
  38. };
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement