<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button onclick="aclick()">抖动测试1</button>
<button onclick="bclick()">抖动测试2</button>
<script>
var b1 = function(){
console.log("bbb");
}
var a1 = function(){
console.log("aaa");
}
var a = debounce(a1,2000)
var b = debounce(b1,2000)
function debounce(fn, wait) {
var timeout = null;
var debounced = function() {
// 用that保存dom2级事件处理中绑定的元素对象
// arg保存默认传给事件处理程序的参数
var that = this,
arg = arguments;
var callNow = !timeout;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(function(){
timeout = null
}, wait)
if(callNow){
fn.apply(that,arg);
};
}
return debounced;
}
function aclick(){
a();
}
function bclick(){
b();
}
</script>
</body>
</html>