From 07ef5122bbc124ecbf3900aacf77ba6d2cb01b9a Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 30 Jan 2025 21:58:07 +0200 Subject: [PATCH] Add fallback timeout to runAfterAnimation --- public/scripts/utils.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/public/scripts/utils.js b/public/scripts/utils.js index a479aee52..f49fe7b64 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -1733,17 +1733,17 @@ export function hasAnimation(control) { /** * Run an action once an animation on a control ends. If the control has no animation, the action will be executed immediately. - * + * The action will be executed after the animation ends or after the timeout, whichever comes first. * @param {HTMLElement} control - The control element to listen for animation end event * @param {(control:*?) => void} callback - The callback function to be executed when the animation ends + * @param {number} [timeout=500] - The timeout in milliseconds to wait for the animation to end before executing the callback */ -export function runAfterAnimation(control, callback) { +export function runAfterAnimation(control, callback, timeout = 500) { if (hasAnimation(control)) { - const onAnimationEnd = () => { - control.removeEventListener('animationend', onAnimationEnd); - callback(control); - }; - control.addEventListener('animationend', onAnimationEnd); + Promise.race([ + new Promise((r) => setTimeout(r, timeout)), // Fallback timeout + new Promise((r) => control.addEventListener('animationend', r, { once: true })), + ]).finally(() => callback(control)); } else { callback(control); }