tonz18's avatar

Livewire 3 debounce not updating

trying to use livewire 3 with debounce.500ms but Its not working with the updated hook.

Livewire Compontent Code


class EmailVerify extends Component
{
    public $code; 

    public function updatedCode() 
    {
        dd($this->code);
    }

    public function render()
    {
        return view('livewire.forms.email-verify');
    }
}

My view Code is:

<div>
        <input
            wire:model.debounce.500ms="code"
            id="verifyemail"
            type="text"
            maxlength="4"
            />
</div>

But if I use wire:model.live="code" it will work but fire on every input.

0 likes
1 reply
LaryAI's avatar
Level 58

It seems like you're trying to use the debounce modifier with Livewire 3 to delay the invocation of the updated hook until 500ms after the user has stopped typing. Your code looks correct at first glance, but let's ensure everything is set up properly.

First, make sure you are using the correct Livewire syntax and that there are no JavaScript errors on your page that could be preventing Livewire from functioning correctly. Open your browser's developer console to check for any errors.

If there are no JavaScript errors, try the following steps:

  1. Clear your browser cache and the Livewire views cache. You can clear the Livewire views cache by running php artisan view:clear in your terminal.

  2. Ensure that Livewire's JavaScript assets are properly included in your layout and that you are not including them multiple times.

  3. Verify that you are using the correct Livewire version that supports the debounce modifier. You can check your Livewire version in your composer.json file or by running composer show livewire/livewire.

  4. If you are using Livewire with Alpine.js, ensure that there are no conflicts between them.

If everything seems to be in order and you're still facing the issue, you can try a different approach to manually implement the debounce functionality. Here's an example of how you can do it:

class EmailVerify extends Component
{
    public $code;
    public $debounceTimer = null;

    public function updatedCode()
    {
        if ($this->debounceTimer) {
            return;
        }

        $this->debounceTimer = true;
        $this->dispatchBrowserEvent('start-debounce');

        $this->debounceTimer = false;
    }

    public function render()
    {
        return view('livewire.forms.email-verify');
    }
}

And in your JavaScript, you can listen for the start-debounce event and use setTimeout to delay the execution:

document.addEventListener('livewire:load', function () {
    window.livewire.on('start-debounce', () => {
        clearTimeout(window.debounceTimer);
        window.debounceTimer = setTimeout(() => {
            window.livewire.emit('debounceTimerFinished');
        }, 500);
    });
});

Then, in your Livewire component, listen for the debounceTimerFinished event:

protected $listeners = ['debounceTimerFinished' => 'onDebounceTimerFinished'];

public function onDebounceTimerFinished()
{
    dd($this->code);
}

This is a more manual approach and should generally not be necessary, but it can serve as a workaround if the built-in debounce modifier is not working as expected.

If none of these solutions work, it might be helpful to check the Livewire documentation for any updates or changes related to the debounce modifier, or consider reaching out to the Livewire community for further assistance.

Please or to participate in this conversation.