javascript 函数限制调用代码


函数:

复制代码 代码如下:

function throttle(fn,ms) {
var last = (new Date()).getTime();
return (function() {
var now = (new Date()).getTime();
if (now - last > ms) {
last = now;
fn.apply(this, arguments);
}
});
}

参数 fn : 传入的函数/方法
参数 ms:每次函数调用时的间隔(毫秒为单位),如输入2000,函数在2秒内不会重复触发。

附一初始化例子

复制代码 代码如下:

document.getElementById('pop').onclick = throttle(function (){
alert(this.id);
},2000)


作用域设为调用者本身
fn.apply(this, arguments);

Examples

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3