Build a countdown timer.
Component
@php
new class extends Livewire\Component {
public int $seconds = 60;
public bool $running = false;
public function start(): void
{
$this->running = true;
}
public function tick(): void
{
if ($this->running && $this->seconds > 0) {
$this->seconds--;
}
if ($this->seconds === 0) {
$this->running = false;
}
}
}
@endphp
<div wire:poll.1s="tick">
<span>{{ gmdate('i:s', $seconds) }}</span>
<button wire:click="start">Start</button>
</div>