Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 0.83 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }