Created
April 4, 2014 01:20
-
-
Save hayes/9966189 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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