gpt4 book ai didi

javascript - 存储带间隔的随机数组值

转载 作者:行者123 更新时间:2023-12-03 12:40:38 24 4
gpt4 key购买 nike

我该如何用白色/无背景替换旧的“盒子”?

var counter = 11;
var check = 0;
var boxes = ["box_1","box_2","box_3","box_4"];

clock = setInterval(function() {
counter--;
if(counter==0) {
clearInterval(clock);
document.getElementById("counter").innerHTML="Time Left: 0 seconds.";
} else {
var box = boxes[Math.floor(Math.random()*boxes.length)];
document.getElementById("counter").innerHTML="Time Left: " + counter.toString() + " seconds.";
document.getElementById(box).style.backgroundColor="#000";
}
}, 1000);

我尝试将盒子存储为 old_box -> old_box = box这不起作用。

看来人们有误解。我想存储框值,以便我可以在下一个循环中回调它并将其设置为白色,而新框可以设置为黑色。

最佳答案

您需要记住该框:

var counter = 11;
var check = 0;
var boxes = ["box_1","box_2","box_3","box_4"];
var lastbox = false;
clock = setInterval(function() {
counter--;
if(counter==0) {
clearInterval(clock);
document.getElementById("counter").innerHTML="Time Left: 0 seconds.";
} else {
if (lastbox) {
document.getElementById(lastbox).style.backgroundColor = "#FFF";
}
lastbox = boxes[Math.floor(Math.random()*boxes.length)];
document.getElementById("counter").innerHTML="Time Left: " + counter.toString() + " seconds.";
document.getElementById(lastbox).style.backgroundColor="#000";
}
}, 1000);

关于javascript - 存储带间隔的随机数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23544109/

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