Skip to main content

Countdown Timer: x-init with Vanilla JavaScript Syntax

Premium
1:51

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

A
alin ✓ Link copied!

Hello again! I ran into a weird situation.

If i follow your example to the letter, all works fine. However, if i choose to do this:

x-init="init()" - i do this because i just like to have the bulk of my functionality inside x-data.

And inside x-data i have the init function what happens is this: init runs twice(because i see the message in the console twice. It decrements the seconds 2 by 2(which makes sense, if init is called twice). Why does that happen?

x-data="{ secondsLeft: 10, init(){ console.log('it runs'); window.setInterval(() => { if(this.secondsLeft > 0) this.secondsLeft = this.secondsLeft - 1; }, 1000); } }"

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.