英文:
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 = options0+网站访问量;
console.log(final);
counter++;
}, 15000);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论