JS连续多次点击事件
需求描述:连续点击五次某个div,跳转到屏保的页面。
// 连续点击五下换出屏保页面
export default function goToScreenProtection() {
let timer = null;
// 该时间间隔内点击才算连续点击(单位:ms)
let waitTime = 200;
// 连续点击次数
let num = 0;
// 最大连续点击次数
let maxNum = 5;
// 上次的点击时间
let lastTime = new Date().getTime();
document.addEventListener("click", () => {
let nowTime = new Date().getTime();
if (nowTime - lastTime < waitTime) {
num++;
} else {
num = 0;
}
lastTime = nowTime;
if (num >= maxNum) {
clearTimeout(timer);
timer = setTimeout(() => {
window.location.href = "/screen-protection";
}, 100);
}
});
}