其一:
function test(){
console.log(0);
}
function throttle(fun){
if(fun.timeoutId) {window.clearTimeout(fun.timeoutId);}
fun.timeoutId = window.setTimeout(function(){
fun();
fun.timeoutId = null;
}, 1000);
}
throttle(test);
throttle(test);
throttle(test);
throttle(test);
其二:
//禁用按钮
$('button').prop('disabled',true);
//回调函数启用按钮
$('button').prop('disabled',false);
如果想要最后一次点击生效,把timer定时器放在函数外每次调用进行检测;如果不涉及定时器,就在函数内部把函数赋值给自己让每次调用都执行新的逻辑
var timer;
var foo = function() {
timer && clearTimeout(timer);
timer = setTimeout(function() {
console.log(0);
}, 5000);
//something
}