F
Filament5mo ago
Zoltar

use pure javascript in livewire

hello, i create a simple javascript timer in blade view How can call method inside the script? i tryed with {{ $this->method() }} but not work
Timer: <label id="seconds">{{$record->tempo_max}}</label>
<input type="hidden" id="contatore" wire:model="tempo_risposta">

<script>
var secondsLabel = document.getElementById("seconds");
var totalSeconds = {{$record->tempo_max}};
setInterval(setTime, 1000);

function setTime() {
--totalSeconds;
secondsLabel.innerHTML = totalSeconds;

var element = document.getElementById('contatore');
element.value = totalSeconds;
element.dispatchEvent(new Event('input'));


if(totalSeconds == 0) {
//ToDo call method
}
}

</script>
Timer: <label id="seconds">{{$record->tempo_max}}</label>
<input type="hidden" id="contatore" wire:model="tempo_risposta">

<script>
var secondsLabel = document.getElementById("seconds");
var totalSeconds = {{$record->tempo_max}};
setInterval(setTime, 1000);

function setTime() {
--totalSeconds;
secondsLabel.innerHTML = totalSeconds;

var element = document.getElementById('contatore');
element.value = totalSeconds;
element.dispatchEvent(new Event('input'));


if(totalSeconds == 0) {
//ToDo call method
}
}

</script>
Solution:
```php <div x-data="counter"> <h1 x-text="count"></h1> </div> ...
Jump to solution
23 Replies
Dennis Koch
Dennis Koch5mo ago
How can call method inside the script?
In what script? setTime() from javascript If you want to "start" that from JS, I think there is a $this->js() or similar method
Zoltar
ZoltarOP5mo ago
yes in setTime method, but need to call livewire action
Dennis Koch
Dennis Koch5mo ago
Ah, you want to call Livewire from JS. That should be $wire.yourMethod() if you are inside a LW component
Zoltar
ZoltarOP5mo ago
I'm inside custom page, I tried like this but if I try to use @script and @endscript by embedding the javascript code it gives me an error
Zoltar
ZoltarOP5mo ago
No description
Zoltar
ZoltarOP5mo ago
can't I use pure javascript code?
Zoltar
ZoltarOP5mo ago
No description
Dennis Koch
Dennis Koch5mo ago
It's an Alpine error so where are you using this inside Alpine? You just showed in in <script> tags?
Zoltar
ZoltarOP5mo ago
not use alpine but pure js
@script
<script>
//let minutesLabel = document.getElementById("minutes");
let secondsLabel = document.getElementById("seconds");
let totalSeconds = {{$record->tempo_max}};
setInterval(setTime, 1000);

function setTime() {
--totalSeconds;
secondsLabel.innerHTML = totalSeconds;
//secondsLabel.innerHTML = pad(totalSeconds % 60);
//minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));

let contatore = document.getElementById('contatore');
contatore.value = totalSeconds;
contatore.dispatchEvent(new Event('input'));

if(totalSeconds == 0) {
let simulate_timer_ko = document.getElementById('simulate_timer_ko');
simulate_timer_ko.dispatchEvent(new Event('click'));
}
}

/*
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
*/
</script>
@endscript
@script
<script>
//let minutesLabel = document.getElementById("minutes");
let secondsLabel = document.getElementById("seconds");
let totalSeconds = {{$record->tempo_max}};
setInterval(setTime, 1000);

function setTime() {
--totalSeconds;
secondsLabel.innerHTML = totalSeconds;
//secondsLabel.innerHTML = pad(totalSeconds % 60);
//minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));

let contatore = document.getElementById('contatore');
contatore.value = totalSeconds;
contatore.dispatchEvent(new Event('input'));

if(totalSeconds == 0) {
let simulate_timer_ko = document.getElementById('simulate_timer_ko');
simulate_timer_ko.dispatchEvent(new Event('click'));
}
}

/*
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
*/
</script>
@endscript
Dennis Koch
Dennis Koch5mo ago
But you error says Alpine Expression Error so it looks like you are using this somewhere inside Alpine? Where are you using that code?
Zoltar
ZoltarOP5mo ago
look screenshot the script is inside a blade view blade view linked to custom page Could it be that within the @script tags I can only use alpine code?
Dennis Koch
Dennis Koch5mo ago
Can you share the whole page? Maybe as a Gist if it gets to big?
Zoltar
ZoltarOP5mo ago
yes wait view_page.php
Zoltar
ZoltarOP5mo ago
Zoltar
ZoltarOP5mo ago
blade view
Zoltar
ZoltarOP5mo ago
Zoltar
ZoltarOP5mo ago
ok try this
Dennis Koch
Dennis Koch5mo ago
Hm. The Blade file looks good to me 🤔
Zoltar
ZoltarOP5mo ago
yes the file is ok and the script work...but I wanted to use wire call inside js. just for code cleanup i use this script but count go down by 2 at time... 20 18 16 14 every one second i tryed with this.count-- and --this.count with same result 🤬
<div>
<div x-data="timer()" x-init="init()">
<div>
<span x-text="count"></span>
</div>
</div>
</div>

<script>
function timer() {
return {
count: {{$record->tempo_max}},
init() {
setInterval(() => {
this.count--;
}, 1000);
},
}
}
</script>
<div>
<div x-data="timer()" x-init="init()">
<div>
<span x-text="count"></span>
</div>
</div>
</div>

<script>
function timer() {
return {
count: {{$record->tempo_max}},
init() {
setInterval(() => {
this.count--;
}, 1000);
},
}
}
</script>
ok fixed...remove x-init="init()"
Solution
Zoltar
Zoltar5mo ago
<div x-data="counter">
<h1 x-text="count"></h1>
</div>

@script
<script>
Alpine.data('counter', () => {
return {
count: {{$record->tempo_max}},
init() {
setInterval(() => {
--this.count;
if(this.count == 0) {
$wire.timer_ko();
}
}, 1000);
},
}
})
</script>
@endscript
<div x-data="counter">
<h1 x-text="count"></h1>
</div>

@script
<script>
Alpine.data('counter', () => {
return {
count: {{$record->tempo_max}},
init() {
setInterval(() => {
--this.count;
if(this.count == 0) {
$wire.timer_ko();
}
}, 1000);
},
}
})
</script>
@endscript
Zoltar
ZoltarOP5mo ago
working script
LeandroFerreira
LeandroFerreira5mo ago
much better than JS 👌
Want results from more Discord servers?
Add your server