I'm showing a toast alert, controlling visibility with alpinejs, and want the alert to dim after a couple of seconds and be hidden after some more seconds unless the mouse hovers over it.
I got the dimming working, but don't know how to interrupt the timeout that hides the element.
<div
x-data="{visible: true, dimmed: false}"
x-show="visible"
x-effect="setTimeout(() => dimmed = true, 5000);setTimeout(() => visible = false, 10000)"
x-on:mouseover="dimmed = false" // <-- How can I stop the second setTimeout() here?
x-transition.duration.500ms
:class="dimmed ? 'opacity-60' : 'opacity-100'"
class="flex alert alert-info justify-between">
<div>Some message here</div>
<div x-on:click="visible = false">
<button class="btn btn-sm btn-square btn-info">
Close
</button>
</div>
</div>
In js I would declare a variable for the setTimeout() and then call cancelTimeout() on that variable but I don't know how to do this using alpine.
Additionally want to restart the setTimeout() once the mouse leaves the <div>. I guess this could easily be done by defining another setTimeout in x-data and then calling that @onmouseleave, but I can't figure out how to interrupt the timeout in the first place.