-2

Using HTML and AlpineJS I have the following:

<div 
  x-data='{ keys: ['a', 'b', 'c'], index: 0 }' 
  x-init="setTimeout(() => i = i < keys.length - 1 ? i + 1 : 0, 4000)">
</div>

I need the index to increment every 4 seconds and back to zero when reaches last one.

My code increments from 0 to 1 but it stops there.

How can I make this work?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

1

You could use setInterval instead of setTimeout since setTimeout will run 1 time after 4 seconds pass after initialization. setInterval will run every 4 seconds and if you need to stop it you can use clearInterval once your needed condition is met.