我们经常会遇到点击事件,但是我们不希望用户短时间内重复点击,直接上代码
html
<button id="button">点我</button>
<script>
function banFastClick(time) {
var lastTime = null,
newTime = null,
flag= 0;
document.getElementById('button').onclick = function () {
newTime = new Date();
if(lastTime !== null){
flag = newTime - lastTime;
flag > time && click()
}else {
click()
}
lastTime = newTime;
}
}
function click() {
console.log('点击一次')
}
banFastClick(500);
</script>