Skip to content

Instantly share code, notes, and snippets.

@hayes
Created April 4, 2014 01:20
Show Gist options
  • Save hayes/9966189 to your computer and use it in GitHub Desktop.
Save hayes/9966189 to your computer and use it in GitHub Desktop.
module.exports = debounce
function debounce(fn, delay, at_start) {
var timeout
if(typeof delay === 'undefined') {
delay = 0
}
return function() {
var args = Array.prototype.slice.call(arguments)
, self = this
if(timeout && at_start) {
return
} else if(!at_start) {
clear()
return timeout = setTimeout(run, delay)
}
timeout = setTimeout(clear, delay)
fn.apply(self, args)
function run() {
clear()
fn.apply(self, args)
}
function clear() {
clearTimeout(timeout)
timeout = null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment