From defec92bf7327fdb6110c28484cdaec15e566cf2 Mon Sep 17 00:00:00 2001 From: yigencong Date: Mon, 8 Apr 2024 07:13:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A4=8D=E4=B9=A0=E8=8A=82=E6=B5=81?= =?UTF-8?q?=E4=B8=8E=E9=98=B2=E6=8A=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html/节流.html | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ html/防抖.html | 36 +++++++++++++++++++ js/节流.js | 87 +++++++++++++++++++++++++++++++++++++++++++++ js/防抖.js | 17 +++++++++ 4 files changed, 236 insertions(+) create mode 100644 html/节流.html create mode 100644 html/防抖.html create mode 100644 js/节流.js create mode 100644 js/防抖.js diff --git a/html/节流.html b/html/节流.html new file mode 100644 index 0000000..a293d16 --- /dev/null +++ b/html/节流.html @@ -0,0 +1,96 @@ + + + + + + 节流 + + +
+
+ + + + \ No newline at end of file diff --git a/html/防抖.html b/html/防抖.html new file mode 100644 index 0000000..fce9b43 --- /dev/null +++ b/html/防抖.html @@ -0,0 +1,36 @@ + + + + + + 防抖 + + +
+ +

+ +
+ + + + \ No newline at end of file diff --git a/js/节流.js b/js/节流.js new file mode 100644 index 0000000..fe35739 --- /dev/null +++ b/js/节流.js @@ -0,0 +1,87 @@ +// 节流是值函数在连续触发的情况下,在规定的时间内,无论如何都会执行一次,比如设置的延迟时间为250 +// 那么如果连续点击1秒钟,那么会执行4四次,而且四次的时间间隔250 + +/** + * @description 节流函数(首节流,第一次触发会立即执行) + * @param { Function } - 要添加节流功能的函数 + * @param { number } - 要延迟的毫秒数 + */ +export function firstThrottle(fun, delay) { + var flag = true // 是否可以执行 + return function(){ + if(!flag) return // 不能执行就直接返回 + // 获取参数和上下文 + var args = arguments + var context = this + fun.apply(context, args) + // 执行完成之后,把flag设置成false表示不能执行,并设置定时器,delay毫秒之后设置true + flag = false + setTimeout(function(){ + flag = true + }, delay); + } +} + +/** + * @description 节流函数(尾节流,无论延迟设置多久,总会执行最后一次) + * @param { Function } - 要添加节流功能的函数 + * @param { number } - 要延迟的毫秒数 + */ +export function lastThrottle(fun, delay) { + var timer = 0 + return function () { + if(timer) return + var args = arguments + var context = this + timer = setTimeout(function () { + fun.apply(context, args) + timer = 0 + },delay) + } +} + +/** + * @description 节流函数(首节流,通过时间戳实现) + * @param { Function } - 要添加节流功能的函数 + * @param { number } - 要延迟的毫秒数 + */ +export function lastThrottleByTime(fun, delay) { + var pre = 0 + return function () { + var cur = Date.now() // 获取当前的时间戳 + if(cur - pre > delay) { + var args = arguments + var context = this + fun.apply(context, args) + pre = cur + } + } +} + +// 思考:首节流适用delay大且对最终状态无要求的功能,缺点是不会执行最终状态。 +// 尾节流适用delay不大且对最终状态有要求的功能,缺点是,当delay大时用户会认为自己的第一次点击好像无效 +// 又没有什么办法可以是首节流,又保留最终状态?之前我们讲过防抖,防抖在高频率出发下,只会触发最后一次,这不就是我们要的尾状态吗! + +/** + * @description 节流函数(首尾节流,通过时间戳实现) + * @param { Function } - 要添加节流功L的函数 + * @param { number } - 要延迟的毫秒数 + */ +export function firstAndLastThrottle(fun, delay) { + var pre = 0 + var timer = 0 + var _delay = delay + 100 // 防抖用的时间间隔,比节流大0.1秒,因为人对0.1秒的延迟无感知 + return function () { + var args = arguments + var context = this + var cur = Date.now() // 获取当前的时间戳 + if(cur - pre > delay) { + fun.apply(context, args) + pre = cur + } + clearTimeout(timer) + timer = setTimeout(() => { + fun.apply(context, args) + }, _delay); + } +} \ No newline at end of file diff --git a/js/防抖.js b/js/防抖.js new file mode 100644 index 0000000..c217478 --- /dev/null +++ b/js/防抖.js @@ -0,0 +1,17 @@ +// 通过防抖,可以避免在用户快速连续地进行操作时(例如连续快速点击按钮),服务器或浏览器因为接收到过多无意义的重复请求而造成的性能瓶颈,从而提高页面性能和用户体验 +/** + * @description 一个防抖函数 + * @param { Function } - 需要添加防抖功能的函数 + * @delay { number } - 防抖延迟的时间 + */ +export default function deBounce(fun, delay) { + var timer = 0 + return function() { + var args = arguments + var context = this + if(timer) clearTimeout(timer) + timer = setTimeout(function(){ + fun.apply(context, args) + }) + } +}