当前位置: 首页 > news >正文

前端页面鼠标移动监控(鼠标运动、鼠标监控)鼠标节流处理、throttle、限制触发频率(setTimeout、clearInterval)

文章目录

    • 使用lodashjs库
    • 手动实现节流(通过判断之前设定的定时器setTimeout是否存在)

使用lodashjs库

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>页面标题</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
    <!-- <link rel="stylesheet" href="styles.css"> -->
    <!-- <script src="script.js" defer></script> -->
    <style>
        .box {
            width: 200px;
            /* 设置盒子的宽度 */
            height: 200px;
            /* 设置盒子的高度 */
            background-color: lightblue;
            /* 设置盒子的背景颜色 */
            border: 1px solid #000;
            /* 可选: 添加边框 */
            display: flex;
            /* 使用 flexbox 布局 */
            justify-content: center;
            /* 水平居中内容 */
            align-items: center;
            /* 垂直居中内容 */
            font-size: 24px;
            /* 设置字体大小 */
        }
    </style>
</head>

<body>
    <div class="box"></div> <!-- 创建一个盒子元素 -->
    <script>
        const box = document.querySelector('.box');
        let i = 1;

        function mouseMove() {
            box.innerHTML = i++; // 显示当前计数器值
            console.log(i);
        }

        // 使用 lodash 的 throttle 函数进行节流
        box.addEventListener('mousemove', _.throttle(mouseMove, 3000)); // 每3000毫秒调用一次
    </script>
</body>

</html>

在这里插入图片描述

手动实现节流(通过判断之前设定的定时器setTimeout是否存在)

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>页面标题</title>
    <!-- <link rel="stylesheet" href="styles.css"> -->
    <!-- <script src="script.js" defer></script> -->
    <style>
        .box {
            width: 200px;
            /* 设置盒子的宽度 */
            height: 200px;
            /* 设置盒子的高度 */
            background-color: lightblue;
            /* 设置盒子的背景颜色 */
            border: 1px solid #000;
            /* 可选: 添加边框 */
            display: flex;
            /* 使用 flexbox 布局 */
            justify-content: center;
            /* 水平居中内容 */
            align-items: center;
            /* 垂直居中内容 */
            font-size: 24px;
            /* 设置字体大小 */
        }
    </style>
</head>

<body>
    <div class="box"></div> <!-- 创建一个盒子元素 -->
    <script>
        const box = document.querySelector('.box');
        let i = 1;

        function mouseMove() {
            box.innerHTML = i++; // 显示当前计数器值
            console.log(i);
        }

        function throttle(fn, delay) {
            let timer = null;
            return function () {
                console.log('判断定时器是否为空?', timer);
                if (!timer) {
                    console.log('定时器为空,延迟执行');
                    timer = setTimeout(() => {
                        fn(); // 在里面就是延迟执行,在外面就是立即执行
                        timer = null;
                    }, delay);
                }
                console.log('不执行');
            }
        }

        // 使用 lodash 的 throttle 函数进行节流
        box.addEventListener('mousemove', throttle(mouseMove, 3000)); // 每3000毫秒调用一次
    </script>
</body>

</html>

在这里插入图片描述

http://www.dtcms.com/a/111928.html

相关文章:

  • 表结构数据的基本特征、获取、加工与使用
  • Java 状态模式 详解
  • 金融机构开源软件生命周期管理实务
  • 模组COF受损制程排查验证及改善
  • 从文本到多模态:如何将RAG扩展为支持图像+文本检索的增强生成系统?
  • 基于 docker 的 Xinference 全流程部署指南
  • shell语言替换脚本、填补整个命令行
  • 知识考量码【蓝桥】
  • leetcode-代码随想录-链表-翻转链表
  • 框架PasteForm实际开发案例,换个口味显示数据,支持echarts,只需要标记几个特性即可在管理端显示(2)
  • Python办公自动化(2)对wordpdf的操作
  • 青少年编程与数学 02-015 大学数学知识点 04课题、微积分
  • 如何判断多个点组成的3维面不是平的,如果不是平的,如何拆分成多个平面
  • 二叉树 递归
  • Linux操作系统 4.Linux实用操作
  • 《新疆建筑安全员C证》考试信息
  • ttkbootstrap 实现日期选择器, 开始和结束时间
  • OrangePi5Plus开发板不能正确识别USB 3.0 设备 (绿联HUB和Camera)
  • Flutter性能优化细节
  • 分子生成的深层次层次变分自编码器 - DrugHIVE 测评
  • Jetpack Compose CompositionLocal 深入解析:局部参数透传实践
  • Linux信号处理解析:从入门到实战
  • 星途(3)
  • C/C++的条件编译
  • 【Tauri2】014——简单使用listen和emit
  • DuckDB系列教程:如何分析Parquet文件
  • Linux中的调试器gdb与冯·诺伊曼体系
  • 使用MCP方案与Claude实现虚幻引擎自动化游戏开发
  • [2008][note]腔内级联拉曼发射的,二极管泵浦多频调Q laser——
  • 【LLM】使用MySQL MCP Server让大模型轻松操作本地数据库