function throttle(fn, interval) {
var timer = null;
var firstTime = true;
var _self = fn;
return function() {
var that = this;
var args = arguments;
// 判断是否第一次执行
if(firstTime) {
_self.apply(that, args);
return firstTime = false;
}
// 判断定时器是否执行完毕
if(timer) {
return false;
}
// 设置定时器
timer = setTimeout(function() {
clearTimeout(timer);
timer = null;
_self.apply(that,args);
}, interval || 500)
}
}
window.onresize = throttle(function() {
console.log('window onresize');
}, 500)