Last active
June 28, 2016 15:31
-
-
Save hashchange/d1ddb2da215aa194519a10119273fe69 to your computer and use it in GitHub Desktop.
Stand-alone, straightforward debounce function. Executes the callback at the end (no option for executing at start).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debounce( callback, delay, context ) { | |
var timer; | |
return function () { | |
var _this = context || this, | |
args = arguments; | |
// Clear a timer which has been created earlier in the sequence of calls. | |
timer && clearTimeout( timer ); | |
// (Re-)start the timer, ie create a new one. | |
timer = setTimeout( function () { | |
callback.apply( _this, args ); | |
}, delay ); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment