Add fallback timeout to runAfterAnimation

07ef5122bbc124ecbf3900aacf77ba6d2cb01b9a

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +7 -7Showing whitespace changes
public/scripts/utils.js+7 -7
@@ -1733,17 +1733,17 @@ export function hasAnimation(control) {
17331733
17341734/**
17351735 * Run an action once an animation on a control ends. If the control has no animation, the action will be executed immediately.
1736- *
1736+ * The action will be executed after the animation ends or after the timeout, whichever comes first.
17371737 * @param {HTMLElement} control - The control element to listen for animation end event
17381738 * @param {(control:*?) => void} callback - The callback function to be executed when the animation ends
1739+ * @param {number} [timeout=500] - The timeout in milliseconds to wait for the animation to end before executing the callback
17391740 */
17401741export function runAfterAnimation(control, callback, timeout = 500) {
17411742 if (hasAnimation(control)) {
1742- const onAnimationEnd = () => {
1743+ Promise.race([
1743- control.removeEventListener('animationend', onAnimationEnd);
1744+ new Promise((r) => setTimeout(r, timeout)), // Fallback timeout
1744- callback(control);
1745+ new Promise((r) => control.addEventListener('animationend', r, { once: true })),
1745- };
1746+ ]).finally(() => callback(control));
1746- control.addEventListener('animationend', onAnimationEnd);
17471747 } else {
17481748 callback(control);
17491749 }