Skip to content

Commit

Permalink
perf(shared): throttle节流修改
Browse files Browse the repository at this point in the history
  • Loading branch information
so11y authored and haoziqaq committed Nov 10, 2021
1 parent 86ae000 commit 981183c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/varlet-ui/src/utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ export const removeItem = (arr: Array<unknown>, item: unknown) => {

export const throttle = (method: any, mustRunDelay = 200): (() => void) => {
let timer: number
let start: number
let start = 0

return function loop(this: unknown, ...args) {
const now = Date.now()
const elapsed = now - start

if (!start) {
start = now
Expand All @@ -94,13 +95,13 @@ export const throttle = (method: any, mustRunDelay = 200): (() => void) => {
window.clearTimeout(timer)
}

if (now - start >= mustRunDelay) {
if (elapsed >= mustRunDelay) {
method.apply(this, args)
start = now
} else {
timer = window.setTimeout(() => {
loop.apply(this, args)
}, 50)
}, mustRunDelay - elapsed)
}
}
}
Expand Down

0 comments on commit 981183c

Please sign in to comment.