mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add fallback timeout to runAfterAnimation
This commit is contained in:
@ -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.
|
* 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 {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 {(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)) {
|
if (hasAnimation(control)) {
|
||||||
const onAnimationEnd = () => {
|
Promise.race([
|
||||||
control.removeEventListener('animationend', onAnimationEnd);
|
new Promise((r) => setTimeout(r, timeout)), // Fallback timeout
|
||||||
callback(control);
|
new Promise((r) => control.addEventListener('animationend', r, { once: true })),
|
||||||
};
|
]).finally(() => callback(control));
|
||||||
control.addEventListener('animationend', onAnimationEnd);
|
|
||||||
} else {
|
} else {
|
||||||
callback(control);
|
callback(control);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user