Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Apr 6, 2024
1 parent 718556f commit 5d5d398
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
43 changes: 27 additions & 16 deletions func/debounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -51,7 +60,9 @@ function debounce (callback, wait, options) {
}
timeout = setTimeout(endFn, wait)
}

debounced.cancel = cancelFn

return debounced
}

Expand Down
43 changes: 27 additions & 16 deletions func/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,59 @@
* @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) {
timeout = setTimeout(endFn, wait)
}
}
}

throttled.cancel = cancelFn

return throttled
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 5d5d398

Please sign in to comment.