从数组中选择一个项,并使下一个被选择的项成为数组中的下一个项。

huangapple go评论76阅读模式
英文:

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

问题

我是相当新的Stack Overflow用户,这是我在这里的第一个问题。如果不清楚的话,对不起!

我想要实现的是从一个数组中选择一个项目,下一个项目是它之后的项目。
我还想每15秒选择一个项目。

我不知道如何做到这一点,所以如果有人能帮助我,那就太好了:D

(我的应用程序是一个基于node.js的Discord机器人)

let options = ["首先选择这个", "然后选择这个", "然后选择这个"]
// 选项

setInterval(function(){
  let final = // 我在这里需要帮助

    // 做事情

}, 15000) // 每15秒
英文:

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 从数组中选择一个项,并使下一个被选择的项成为数组中的下一个项。

(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

答案1

得分: 1

让我来帮你翻译这段代码:

一种方法是使用一个计数变量来跟踪你想要选择的数组中的项目然后你可以递增计数器
这里的 `%` 运算符用于将计数器重置回起始位置

let options = ["这将被首先选择", "这将被其次选择", "这将被第三选择"];
let counter = 0;

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

  counter++;
}, 15000);
英文:

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
0
+
网站访问量
; console.log(final); counter++; }, 15000);

huangapple
  • 本文由 发表于 2023年1月9日 17:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定