diff --git a/func/debounce.js b/func/debounce.js index 87016ba..d15102e 100644 --- a/func/debounce.js +++ b/func/debounce.js @@ -7,42 +7,51 @@ * @return {Function} */ function debounce (callback, wait, options) { - var args, context + var args = null + var context = null var opts = options || {} var runFlag = false - var isDestroy = false - var timeout = 0 + var timeout = null var isLeading = typeof options === 'boolean' var optLeading = 'leading' in opts ? opts.leading : isLeading var optTrailing = 'trailing' in opts ? opts.trailing : !isLeading + + var gcFn = function () { + args = null + context = null + } + var runFn = function () { - if (!isDestroy) { - runFlag = true - timeout = 0 - callback.apply(context, args) - } + runFlag = true + callback.apply(context, args) + gcFn() } + var endFn = function () { if (optLeading === true) { - timeout = 0 + timeout = null } - if (!isDestroy && !runFlag && optTrailing === true) { + if (!runFlag && optTrailing === true) { runFn() } } + var cancelFn = function () { - var rest = timeout !== 0 - clearTimeout(timeout) - args = null - context = null - timeout = 0 + var rest = timeout !== null + if (rest) { + clearTimeout(timeout) + } + gcFn() + timeout = null + runFlag = false return rest } + var debounced = function () { runFlag = false args = arguments context = this - if (timeout === 0) { + if (timeout === null) { if (optLeading === true) { runFn() } @@ -51,7 +60,9 @@ function debounce (callback, wait, options) { } timeout = setTimeout(endFn, wait) } + debounced.cancel = cancelFn + return debounced } diff --git a/func/throttle.js b/func/throttle.js index 97d2549..29577ba 100644 --- a/func/throttle.js +++ b/func/throttle.js @@ -7,40 +7,49 @@ * @return {Function} */ function throttle (callback, wait, options) { - var args, context + var args = null + var context = null var opts = options || {} var runFlag = false - var isDestroy = false - var timeout = 0 + var timeout = null var optLeading = 'leading' in opts ? opts.leading : true var optTrailing = 'trailing' in opts ? opts.trailing : false + + var gcFn = function () { + args = null + context = null + } + var runFn = function () { - if (!isDestroy) { - runFlag = true - callback.apply(context, args) - timeout = setTimeout(endFn, wait) - } + runFlag = true + callback.apply(context, args) + timeout = setTimeout(endFn, wait) + gcFn() } + var endFn = function () { - timeout = 0 - if (!isDestroy && !runFlag && optTrailing === true) { + timeout = null + if (!runFlag && optTrailing === true) { runFn() } } + var cancelFn = function () { - var rest = timeout !== 0 - clearTimeout(timeout) - args = null - context = null + var rest = timeout !== null + if (rest) { + clearTimeout(timeout) + } + gcFn() + timeout = null runFlag = false - timeout = 0 return rest } + var throttled = function () { args = arguments context = this runFlag = false - if (timeout === 0) { + if (timeout === null) { if (optLeading === true) { runFn() } else if (optTrailing === true) { @@ -48,7 +57,9 @@ function throttle (callback, wait, options) { } } } + throttled.cancel = cancelFn + return throttled } diff --git a/package.json b/package.json index a9f35c9..1a7911a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xe-utils", - "version": "3.5.23", + "version": "3.5.24", "description": "JavaScript 函数库、工具类", "main": "index.js", "unpkg": "dist/xe-utils.umd.min.js",