Skip to content

Instantly share code, notes, and snippets.

@sagiavinash
Created April 28, 2015 17:14
Show Gist options
  • Save sagiavinash/9052abf217736ad9384b to your computer and use it in GitHub Desktop.
Save sagiavinash/9052abf217736ad9384b to your computer and use it in GitHub Desktop.
Throttle Snippet - Jquery
(function($) {
$.extend({
throttle : function(fn, timeout, ctx) {
var timer, args, needInvoke;
return function() {
args = arguments;
needInvoke = true;
ctx = ctx || this;
if(!timer) {
(function() {
if(needInvoke) {
fn.apply(ctx, args);
needInvoke = false;
timer = setTimeout(arguments.callee, timeout);
} else {
timer = null;
}
})();
}
};
}
});
})(jQuery);
$(window).on('resize', $.throttle(function() {
// All the taxing stuff you do
}, 250));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment