首页 > 解决方案 > 命令线:jQuery中的poll.1000ms

问题描述

如何在livewire使用 jQuery 代码时执行命令 ‍‍‍‍<code>wire: poll.10000ms。我使用 jQuery 在视图内部检查输入值不为空,然后必须执行此命令。可能吗?

if (! $('#unitValue').val().length === 0) {
       //Run wire:poll.10000ms="reUnit" with Jquery code
}

标签: laravellaravel-livewire

解决方案


您可能会做一些wire:poll.10000ms="reUnit"事情来setInterval将.eventLivewireComponent

活线组件

protected $listeners = [
    'reUnit'
];

public function reUnit()
{
    // do something here
}

刀片文件

<script>
    document.addEventListener('DOMContentLoaded', function (e) {
        setInterval(() => Livewire.emit('reUnit'), 10000);
    })
</script>

我们在这里所做的只是创建一个ticks每 10 秒一次的计时器,然后触发我们提供的函数,在本例中是一个Livewire事件。


推荐阅读