Skip to content

Instantly share code, notes, and snippets.

@haribote
Last active August 29, 2015 14:00
Show Gist options
  • Save haribote/11028417 to your computer and use it in GitHub Desktop.
Save haribote/11028417 to your computer and use it in GitHub Desktop.
/*
* jquery.reduce.js
* - 実行回数を間引くユーティリティ
* - jQuery は名前空間を借りているだけ
*/
;(function (window, $, undefined) {
$.extend({
throttle: function(func, delay) {
var context = null;
var args = [];
var timer = null;
var _func = function() {
timer = null;
func.apply(context, args)
context = null;
args = [];
};
return function () {
if (!timer) {
context = this;
args = arguments;
timer = window.setTimeout(_func, delay);
}
};
},
debounce: function(func, delay) {
var context = null;
var args = [];
var timer = null;
var _func = function() {
func.apply(context, args);
context = null;
args = [];
};
return function() {
context = this;
args = arguments;
if (timer) {
window.clearTimeout(timer);
}
timer = window.setTimeout(_func, delay);
};
}
});
})(this, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment