JS连续多次点击事件

这篇博客介绍了一个JavaScript实现的功能,当用户在指定时间内连续点击div元素五次时,页面将跳转到屏保页面。通过设置定时器和判断点击间隔,实现了连续点击的计数和时间限制,从而确保了连续点击的效果。
摘要由CSDN通过智能技术生成

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);
    }
  });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值