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

Time:01-09

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 :D

(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

CodePudding user response:

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);
  • Related