Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. /**
  2. * timedInterval
  3. *
  4. * The method calls a function or evaluates an expression at specified intervals (in milliseconds)
  5. * until a specified number of milliseconds.
  6. *
  7. * @param {Function} callback
  8. * @param {int} interval - run every [interval] ms
  9. * @param {int} expiration - run until [expiration] in ms
  10. * @param {mixed} callbackArgs - arguments for callback function
  11. * @return {function}
  12. */
  13. function timedInterval(callback, interval, expiration, callbackArgs = null) {
  14. var wait = false;
  15.  
  16. return function() {
  17.  
  18. if (wait) {
  19. return;
  20. }
  21.  
  22. wait = true;
  23.  
  24. var handle = setInterval(function() {
  25. callback.call();
  26. }, interval);
  27.  
  28. setTimeout(function() {
  29. clearInterval(handle);
  30. wait = false;
  31. }, expiration);
  32. };
  33. };
  34.  
  35. // Example:
  36. var onScroll = timedInterval(function(){
  37. // code
  38. }, 500, 10000, null);
  39.  
  40. $( window ).on( "scroll", onScroll );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement