Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select a item from a array and have the next item that is selected be the next item in the array

I am pretty new to stack overflow and this is my first question here. Sorry if it wasn’t clear!

I want to make it so that a item is picked from a array and the next item is the item after it
I also want to pick a item every 15 seconds

I am lost in how to do this so if someone could help me it would be nice 😀

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(my application is a Discord Bot in node.js)

let options = ["This will be picked first", "This will be picked second", "This will be picked third"]
// the options

    setInterval(function(){
      let final = // I need help here

        // do things

  }, 15000) // every 15 seconds

What I want this code to do, is first pick the first option and then 15 seconds later, pick the second option and then pick the third option and repeat

>Solution :

one way is to have a counter variable to keep track of the items in the array you want to pick next, then you can increment the counter
the % operator here is used to reset counter back to beginning

let options = ["This will be picked first", "This will be picked second", "This will be picked third"];
let counter = 0;

setInterval(function() {
  let final = options[counter % options.length];
  console.log(final);

  counter++;
}, 15000);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading