失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > JS连续多次点击事件

JS连续多次点击事件

时间:2022-03-07 15:50:23

相关推荐

JS连续多次点击事件

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);}});}

如果觉得《JS连续多次点击事件》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。