JS返回数组中的next,无限循环
我有一个像这样的字符串列表;
var mystrings = [
'apple',
'banana',
'orange'
]
我想要一个可以随时调用来获取下一个字符串的函数。 当到达列表末尾时,重新开始并再次获取第一个。
我将它用于必须按列表中的顺序应用的 CSS 类列表,但在需要时我不会循环遍历该列表。
我无法弄清楚,而且谷歌搜索也很难。 有什么想法吗?
I have a list of strings like so;
var mystrings = [
'apple',
'banana',
'orange'
]
I would like a function I can call at anytime to get the next string.
And when the end of the list is reached, start over and get the first one again.
I am using it for a list of CSS classes that must be applied in the order from the list, but I will not be looping over that list when they are needed.
I can't figure it out and it is somehow hard to google.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个有趣的小函数:
像这样调用:
Here's a fun little function:
called like this:
使用外部索引变量并在每次调用时将其递增或重置为 0
Use an external index variable and increment or reset it to 0 on each call