From 8178359a080df8c859d01eacda0d75a95329c989 Mon Sep 17 00:00:00 2001 From: pjxxcc <562844621@qq.com> Date: Thu, 30 Jun 2022 15:43:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dthrottle=E9=97=AD?= =?UTF-8?q?=E5=8C=85=E5=AF=BC=E8=87=B4=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此throttle函数存储了最后一次触发事件时的参数arguments和context,导致如果后续不再触发此事件,那么上一次触发事件时的dom以及组件都无法被内存回收。 比如在vxe-table中,触发mouse-wheel后表格会被缓存,后面及时销毁了表格,其内存也无法被释放。当然因为mouse-wheel事件是注册在window上的,在空白区域再次滚动鼠标滚轮,内存就会得以释放。 --- func/throttle.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/func/throttle.js b/func/throttle.js index 5a8cf73d..c0a5e611 100644 --- a/func/throttle.js +++ b/func/throttle.js @@ -7,21 +7,20 @@ * @return {Function} */ function throttle (callback, wait, options) { - var args, context var opts = options || {} var runFlag = false var timeout = 0 var optLeading = 'leading' in opts ? opts.leading : true var optTrailing = 'trailing' in opts ? opts.trailing : false - var runFn = function () { + var runFn = function (args, context) { runFlag = true callback.apply(context, args) timeout = setTimeout(endFn, wait) } - var endFn = function () { + var endFn = function (args, context) { timeout = 0 if (!runFlag && optTrailing === true) { - runFn() + runFn(args, context) } } var cancelFn = function () { @@ -37,9 +36,9 @@ function throttle (callback, wait, options) { runFlag = false if (timeout === 0) { if (optLeading === true) { - runFn() + runFn(args, context) } else if (optTrailing === true) { - timeout = setTimeout(endFn, wait) + timeout = setTimeout(() => endFn(args, context), wait) } } } From 300819f5fd08a285d54c177452404df99632b2e8 Mon Sep 17 00:00:00 2001 From: pjxxcc <562844621@qq.com> Date: Mon, 4 Jul 2022 11:40:06 +0800 Subject: [PATCH 2/2] Update throttle.js --- func/throttle.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/func/throttle.js b/func/throttle.js index c0a5e611..c829ccb5 100644 --- a/func/throttle.js +++ b/func/throttle.js @@ -31,8 +31,8 @@ function throttle (callback, wait, options) { return rest } var throttled = function () { - args = arguments - context = this + const args = arguments + const context = this runFlag = false if (timeout === 0) { if (optLeading === true) {