核心记忆点:在判断flag的if下设置flag=false。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btn">提交</button>
<script>
var oBtn = document.getElementById("btn");
oBtn.onclick = throttle();
function show() {
setTimeout(function(){
console.log("模拟的ajax执行了!");
},3000)
}
function throttle(){
var flag = true;
return function(){
if(!flag) return ;
flag = false;
timer = setTimeout(function(){
show();
flag = true;
},500)
}
}
</script>
</body>
</html>