Skip to content

Instantly share code, notes, and snippets.

@lichenbuliren
Created September 13, 2016 09:19
Show Gist options
  • Save lichenbuliren/aaa9b700e2ff861cfc9a8f2817011ec6 to your computer and use it in GitHub Desktop.
Save lichenbuliren/aaa9b700e2ff861cfc9a8f2817011ec6 to your computer and use it in GitHub Desktop.
/**
* 函数节流实现
* @param {Function} fn 需要节流执行的函数
* @param {[type]} interval 事件执行间隔时间,单位 ms
* @return {[type]} [description]
*/
var throttle = function(fn, interval) {
var _self = fn,
timer,
firstTime = true;
console.log(_self);
return function() {
var args = arguments,
_me = this; // 这里代表当前的匿名函数
console.log(_me);
if (firstTime) {
_self.apply(_me, args);
return firstTime = false;
}
if (timer) {
return false;
}
timer = setTimeout(function() {
clearTimeout(timer);
timer = null;
_self.apply(_me, args);
}, interval || 500);
};
};
window.onresize = throttle(function() {
console.log('test');
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment