gpt4 book ai didi

javascript - 同时运行多个超时

转载 作者:行者123 更新时间:2023-12-03 10:14:27 26 4
gpt4 key购买 nike

有没有办法在 switch 语句之间移动,以便两个 div 都会循环?

我正在编写一个桌面应用程序,它会根据您输入的兴趣推荐不同的事情。现在,我们有音乐和视频游戏类型。它们是通过使用下拉菜单来选择的。这些流派都有自己的功能。这些函数在下拉菜单发生变化时调用,如下所示

<select id="vg" class="dropdown" onchange="vgchosen.call(this, event)">
<select id="music" class="dropdown" onchange="muchosen.call(this, event)">

选择类型后,div 标签开始循环文本,为您提供有关该怎么做的想法。

<div id="vgc" class="genres">Game Genres Here</div>
<div id="muc" class="genres">Music Genres Here</div>

这里可以看到一些循环文本

case "Action":
var actionvgc = [
"You should play |Skyrim",
"You should play |Killing Floor 2",
"You Should play |Counter Strike Global Offensive",
];
clearInterval(timerId);
timerId = setInterval(function() { var i = Math.round((Math.random()) * actionvgc.length);
if (i == actionvgc.length) --i;
$("#vgc").html(actionvgc[i]); }, 1000);
break;

case "Pop":
var popmuc = [
"You should listen to |Maroon 5",
"You should listen to |Nicki Minaj",
"You Should listen to |5 Seconds of Summer",
];
clearInterval(timerId);
timerId = setInterval(function() { var i = Math.round((Math.random()) * popmuc.length);
if (i == popmuc.length) --i;
$("#muc").html(popmuc[i]); }, 1000);
break;

当您第一次选择类型时,一切都会正常,文本会循环播放,每个人都很高兴。当您尝试从其他下拉菜单中选择一种类型时,就会出现问题。例如:你选择了 Action 游戏,你看到了 Action 游戏。你选择流行音乐,你看到流行音乐,但 Action 游戏已经停止循环。

最佳答案

你应该设置两个不同的timerId间隔,因为你这样做的方式,无论选择音乐还是游戏,你总是会清除之前设置的间隔:

var timerIdForGames, timerIdForMusic;
case "Action":
var actionvgc = [
"You should play |Skyrim",
"You should play |Killing Floor 2",
"You Should play |Counter Strike Global Offensive",
];
clearInterval(timerIdForGames);
timerIdForGames = setInterval(function() { var i = Math.round((Math.random()) * actionvgc.length);
if (i == actionvgc.length) --i;
$("#vgc").html(actionvgc[i]); }, 1000);
break;

case "Pop":
var popmuc = [
"You should listen to |Maroon 5",
"You should listen to |Nicki Minaj",
"You Should listen to |5 Seconds of Summer",
];
clearInterval(timerIdForMusic);
timerIdForMusic = setInterval(function() { var i = Math.round((Math.random()) * popmuc.length);
if (i == popmuc.length) --i;
$("#muc").html(popmuc[i]); }, 1000);
break;

关于javascript - 同时运行多个超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29951131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com