IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 防抖与节流 -> 正文阅读

[JavaScript知识库]防抖与节流

目录

应用场景

1. 通过使用underscore loadash等前端工具库里封装的debounce和throttle函数

防抖

节流

2. 自己封装debounce和throttle函数

防抖

节流


防抖与节流属于高级函数的内容,用来解决事件的频繁发生而导致的服务器的宕机。

应用场景

1.scroll事件滚动触发

2.搜索框输入查询

3.表单验证

4.按钮提交事件

1. 通过使用underscore loadash等前端工具库里封装的debounce和throttle函数

防抖

<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        #container {
            height: 200px;
            background-color: rgb(41, 39, 39);
            color: #fff;
            text-align: center;
            line-height: 200px;
            font-style: 30px;
        }
    </style>
    <script src="./underscore.js"></script>
</head>

<body>
    <!-- dom容器 -->
    <div id="container"></div>
    <script>
        // 演示事件如何频繁发生
        // 获取dom容器
        let container = document.querySelector('#container');
        // 绑定事件,触发事件频繁发生
        let count = 1;
        function doSomeThing(e) {
            console.log(this); //window
            console.log(e); //undefined
            // 可能做回调或者是ajax请求
            container.innerHTML = count++;
        }
        // 事件处理函数(doSomeThing)在一定时间(1000)后才执行
        // 如果这段时间再次调用,则会重新计算时间,如果超过预定时间,则执行事件处理函数
        container.onmousemove = _.debounce(doSomeThing, 1000);
    </script>
</body>

</html>

节流

<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        #container {
            height: 200px;
            background-color: rgb(41, 39, 39);
            color: #fff;
            text-align: center;
            line-height: 200px;
            font-style: 30px;
        }
    </style>
    <script src="./underscore.js"></script>
</head>

<body>
    <!-- dom容器 -->
    <div id="container"></div>
    <script>
        // 演示事件如何频繁发生
        // 获取dom容器
        let container = document.querySelector('#container');
        // 绑定事件,触发事件频繁发生
        let count = 1;
        function doSomeThing(e) {
            console.log(this); //window
            console.log(e); //undefined
            // 可能做回调或者是ajax请求
            container.innerHTML = count++;
        }
        container.onmousemove = _.throttle(doSomeThing, 1000);
    </script>
</body>

</html>

2. 自己封装debounce和throttle函数

防抖

<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        #container {
            height: 200px;
            background-color: rgb(41, 39, 39);
            color: #fff;
            text-align: center;
            line-height: 200px;
            font-style: 30px;
        }
    </style>
</head>

<body>
    <!-- dom容器 -->
    <div id="container"></div>
    <script>
        // 演示事件如何频繁发生
        // 获取dom容器
        let container = document.querySelector('#container');
        // 绑定事件,触发事件频繁发生
        let count = 1;
        function doSomeThing(e) {
            console.log(this); //window
            console.log(e); //undefined
            // 可能做回调或者是ajax请求
            container.innerHTML = count++;
        }
        // 事件处理函数(doSomeThing)在一定时间(1000)后才执行
        // 如果这段时间再次调用,则会重新计算时间,如果超过预定时间,则执行事件处理函数
        container.onmousemove = debounce(doSomeThing, 1000);
        // 自己封装防抖函数
        function debounce(fun, wait) {
            let timeout;
            return function () {
                // console.log(this); dom节点
                // console.log(arguments); //mousemove{}
                let argus = arguments;
                let context = this;
                clearTimeout(timeout)
                timeout = setTimeout(function () {
                    fun.apply(context, argus);//doSomeThing
                }, wait)
            }
        }
    </script>
</body>

</html>

节流

  • 时间戳
<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        #container {
            height: 200px;
            background-color: rgb(41, 39, 39);
            color: #fff;
            text-align: center;
            line-height: 200px;
            font-style: 30px;
        }
    </style>
</head>

<body>
    <!-- dom容器 -->
    <div id="container"></div>
    <script>
        // 演示事件如何频繁发生
        // 获取dom容器
        let container = document.querySelector('#container');
        // 绑定事件,触发事件频繁发生
        let count = 1;
        function doSomeThing(e) {
            console.log(this); //window
            console.log(e); //undefined
            // 可能做回调或者是ajax请求
            container.innerHTML = count++;
        }
        container.onmousemove = throttle(doSomeThing, 1000);
        // 节流原理:如果持续触发事件,每隔一段时间,执行一次事件
        // 自己封装节流函数 --时间戳 超时调用
        // 时间戳
        function throttle(fun, wait) {
            // 定义一个旧的时间戳
            let old = 0;
            let context, argus;
            return function () {
                // 获取一下当前的时间戳
                let now = new Date().valueOf();
                // console.log(now);
                if (now - old > wait) {
                    // 立即执行
                    fun()
                    old = now;
                }
            }
        }

    </script>
</body>

</html>
  • 超时调用
<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        #container {
            height: 200px;
            background-color: rgb(41, 39, 39);
            color: #fff;
            text-align: center;
            line-height: 200px;
            font-style: 30px;
        }
    </style>
</head>

<body>
    <!-- dom容器 -->
    <div id="container"></div>
    <script>
        // 演示事件如何频繁发生
        // 获取dom容器
        let container = document.querySelector('#container');
        // 绑定事件,触发事件频繁发生
        let count = 1;
        function doSomeThing(e) {
            console.log(this); //window
            console.log(e); //undefined
            // 可能做回调或者是ajax请求
            container.innerHTML = count++;
        }
        container.onmousemove = throttle(doSomeThing, 1000);
        // 节流原理:如果持续触发事件,每隔一段时间,执行一次事件
        // 自己封装节流函数 --时间戳 超时调用
        // 超时调用
        function throttle(fun, wait) {
            let context, argus, timeout;
            return function () {
                context = this;
                argus = arguments;
                if (!timeout) {
                    timeout = setTimeout(() => {
                        fun.apply(context, argus);
                        timeout = null;
                    }, wait)
                }
            }
        }

    </script>
</body>

</html>

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-21 12:06:15  更:2021-10-21 12:07:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/12 22:29:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码