Skip to content

fix(runtime-dom): clear setTimeOut #2520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions packages/runtime-dom/src/components/Transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function resolveTransitionProps(
}

const makeEnterHook = (isAppear: boolean) => {
return (el: Element, done: () => void) => {
return (el: Element & { _timeoutID?: number }, done: () => void) => {
const hook = isAppear ? onAppear : onEnter
const resolve = () => finishEnter(el, isAppear, done)
hook && hook(el, resolve)
Expand All @@ -127,7 +127,10 @@ export function resolveTransitionProps(
addTransitionClass(el, isAppear ? appearToClass : enterToClass)
if (!(hook && hook.length > 1)) {
if (enterDuration) {
setTimeout(resolve, enterDuration)
el._timeoutID = (setTimeout(
resolve,
enterDuration
) as unknown) as number
} else {
whenTransitionEnds(el, type, resolve)
}
Expand All @@ -149,7 +152,7 @@ export function resolveTransitionProps(
},
onEnter: makeEnterHook(false),
onAppear: makeEnterHook(true),
onLeave(el, done) {
onLeave(el: Element & { _timeoutID?: number }, done) {
const resolve = () => finishLeave(el, done)
addTransitionClass(el, leaveActiveClass)
addTransitionClass(el, leaveFromClass)
Expand All @@ -158,23 +161,29 @@ export function resolveTransitionProps(
addTransitionClass(el, leaveToClass)
if (!(onLeave && onLeave.length > 1)) {
if (leaveDuration) {
setTimeout(resolve, leaveDuration)
el._timeoutID = (setTimeout(
resolve,
leaveDuration
) as unknown) as number
} else {
whenTransitionEnds(el, type, resolve)
}
}
})
onLeave && onLeave(el, resolve)
},
onEnterCancelled(el) {
onEnterCancelled(el: Element & { _timeoutID?: number }) {
el._timeoutID && clearTimeout(el._timeoutID)
finishEnter(el, false)
onEnterCancelled && onEnterCancelled(el)
},
onAppearCancelled(el) {
onAppearCancelled(el: Element & { _timeoutID?: number }) {
el._timeoutID && clearTimeout(el._timeoutID)
finishEnter(el, true)
onAppearCancelled && onAppearCancelled(el)
},
onLeaveCancelled(el) {
onLeaveCancelled(el: Element & { _timeoutID?: number }) {
el._timeoutID && clearTimeout(el._timeoutID)
finishLeave(el)
onLeaveCancelled && onLeaveCancelled(el)
}
Expand Down Expand Up @@ -248,7 +257,7 @@ function nextFrame(cb: () => void) {
}

function whenTransitionEnds(
el: Element,
el: Element & { _timeoutID?: number },
expectedType: TransitionProps['type'] | undefined,
cb: () => void
) {
Expand All @@ -257,10 +266,10 @@ function whenTransitionEnds(
return cb()
}

const endEvent = type + 'end'
const endEvent = 'on' + type + 'end'
let ended = 0
const end = () => {
el.removeEventListener(endEvent, onEnd)
;(el as any)[endEvent] = null
cb()
}
const onEnd = (e: Event) => {
Expand All @@ -270,12 +279,12 @@ function whenTransitionEnds(
}
}
}
setTimeout(() => {
el._timeoutID = (setTimeout(() => {
if (ended < propCount) {
end()
}
}, timeout + 1)
el.addEventListener(endEvent, onEnd)
}, timeout + 1) as unknown) as number
;(el as any)[endEvent] = onEnd
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里如果使用 addEventListener 会导致快速点击时注册多个 onEnd ,在动画结束时这些 onEnd 会一起被调用。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the better way is to just clearTimeout before setTimeout.

}

interface CSSTransitionInfo {
Expand Down