javascript一些方法兼容
标签(空格分隔): javascript 方法收集
Object.keys
if (!Object.keys) Object.keys = function(o) {
if (o !== Object(o))
throw new TypeError('Object.keys called on a non-object');
var k=[],p;
for (p in o) if (Object.prototype.hasOwnProperty.call(o,p)) k.push(p);
return k;
}
throttle
让函数必须间隔多少时间执行,函数节流,在movemove等需要一停执行进有用,再比如window.resize
function throttle(fn,delay) {
var timer = null;
var t_start;
delay = delay||200;
return function() {
var context = this,t_curr = +new Date();
if(!t_start) {
t_start = t_curr;
}
var args = arguments;
if(t_curr - t_start <delay) {
clearTimeout(timer);
timer = setTimeout(function(){
fn.apply(context,args);
},delay)
}else {
fn.apply(context,args);
t_start = t_curr;
}
}
}