Created
September 24, 2012 17:11
-
-
Save eldargab/3777055 to your computer and use it in GitHub Desktop.
throttle function
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
function throttle (fn, timeout) { | |
var ctx, args, wait = false | |
return function () { | |
if (wait) { | |
ctx = this | |
args = arguments | |
return | |
} | |
wait = true | |
fn.apply(this, arguments) | |
setTimeout(function () { | |
wait = false | |
var _ctx = ctx | |
var _args = args | |
ctx = null | |
args = null | |
_args && fn.apply(_ctx, _args) | |
}, timeout) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment