Guest User

Untitled

a guest
Aug 6th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. /**
  2. * Class allowing to call a method only after a specific delay if no other call was made
  3. * It is useful to prevent triggering the same callback multiple times quickly. For instance
  4. * ajax calls on keyup. It introduces a delay before triggering the callback
  5. *
  6. * Usage:
  7. * var refreshResults = new DelayedCall(1000, function() { alert('Mouse not moved for 1 second'); })
  8. * $('body').mousemove(function() { refreshResults.trigger(); })
  9. *
  10. * @param delay Inactivity delay in millisecond before triggering the callback
  11. * @param callback Callback to trigger after a specific delay
  12. * @return void
  13. */
  14. function DelayedCall(delay, callback) {
  15. this.delay = delay;
  16. this.callback = callback;
  17. this.timeout = null;
  18.  
  19. this.trigger = function() {
  20. window.clearTimeout(this.timeout);
  21. this.timeout = setTimeout(this.callback, this.delay);
  22. };
  23. }
Advertisement
Add Comment
Please, Sign In to add comment