Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

waachosen-will's avatar

Dispatching the same event before the previous event ends

I'm trying to set up a dispatch event for flash messages. I've currently got it working with the following code.

$this->dispatch('flash-message', message: 'Attendee ' . $this->form->attendeeName . ' (' . $this->form->attendeeEmail . ')' . ' successfully added.');
<div class="container">
    <div class="p-4 mx-auto w-full fixed bottom-0 text-sm text-yellow-800 rounded-lg bg-yellow-100 dark:bg-gray-800 dark:text-yellow-300"
         x-data="{ show: false, message: '' }" x-show="show"
         x-init="$watch('show', value => { if (value) setTimeout(() => show = false, 3000) })"
         x-transition:leave.opacity.duration.3000ms
         x-on:flash-message.window="show = true; message = $event.detail.message"
         x-text="message"
         role="alert">
    </div>
</div>

My issue is that if I try to fire multiple of the same event before the previous event has finished, although the data in the message is updated, the transition doesn't reset. This means if I chain together some quickfire updates, after the first event is fired and the flash message transitions out, I'm not seeing any of the new flash messages unless I dispatch another event after the entire chain is finished (e.g. if I fire 5 events quickly, I have to wait 15 seconds from the first event before I can fire one that shows) . I think it's related to the setTimeout I have in the x-init, which is needed for the transition to work.

Is there anyway for me to reset the transition sequence when a new event is fired or to delay any new events until the previous one is finished?

0 likes
1 reply
jj15's avatar

When building a flash message (or "toasts") system, you'd typically handle this by showing multiple toasts, not just one with a message that gets swapped out.

I don't have any concrete code to share, but I'd assume you'd create an Alpine component that listens for the flash-message event, stores it within an array inside x-data, and then uses x-for to loop through and create a new toast element.

Instead of building it yourself, you could also use a package like this one.

Please or to participate in this conversation.