我如何更改我的代码以同时启动我的 SetTimeout?
How do i change my code to start my SetTimeout at the same time?
如何更改我的代码以同时开始闪烁,并且仍然是无限的?
功能眨眼(){
如果(!toggleSwitch){
for (let i = 0; i < elements.length; i++) {
shapes.push(elements[i].className);
}
// Start off at the first element.
let i = 0;
let len = shapes.length;
// Do the next link
function doNext() {
let element = shapes[i];
i++;
eval(element).fillColor = eval(element).setColor;
document.getElementById(element).style.backgroundColor = (eval(element).fillColor===document.getElementById(element).style.backgroundColor) ? 'white' : eval(element).setColor ;
if (i < len) {
// Don't do anything special
} else {
// Reset the counter
i = 0;
}
myVar = setTimeout(doNext, 1000); }
// And the code needs kicked off somewhere
doNext();
}
}
此外,我无法切换 类,因为通过拖放,我可以在 运行 时间内随时更改背景颜色,因此我为每个形状都有一个对象,我保留背景颜色。 –
我想你要找的是setInterval
。请参阅下面的基本功能闪烁示例。
let red = true;
let squares = document.querySelectorAll('.square');
setInterval(() => {
red = !red;
for (let i = 0; i < squares.length; i++) {
if (red) squares[i].className = 'square red';
else squares[i].className = 'square';
}
}, 2000);
.square {
height: 50px;
width:50px;
margin:10px;
background-color: #FFF;
}
.red {
background-color: #F00;
}
<div class="square red"></div>
<div class="square red"></div>
<div class="square red"></div>
<div class="square red"></div>
如何更改我的代码以同时开始闪烁,并且仍然是无限的?
功能眨眼(){ 如果(!toggleSwitch){
for (let i = 0; i < elements.length; i++) {
shapes.push(elements[i].className);
}
// Start off at the first element.
let i = 0;
let len = shapes.length;
// Do the next link
function doNext() {
let element = shapes[i];
i++;
eval(element).fillColor = eval(element).setColor;
document.getElementById(element).style.backgroundColor = (eval(element).fillColor===document.getElementById(element).style.backgroundColor) ? 'white' : eval(element).setColor ;
if (i < len) {
// Don't do anything special
} else {
// Reset the counter
i = 0;
}
myVar = setTimeout(doNext, 1000); }
// And the code needs kicked off somewhere
doNext();
}
} 此外,我无法切换 类,因为通过拖放,我可以在 运行 时间内随时更改背景颜色,因此我为每个形状都有一个对象,我保留背景颜色。 –
我想你要找的是setInterval
。请参阅下面的基本功能闪烁示例。
let red = true;
let squares = document.querySelectorAll('.square');
setInterval(() => {
red = !red;
for (let i = 0; i < squares.length; i++) {
if (red) squares[i].className = 'square red';
else squares[i].className = 'square';
}
}, 2000);
.square {
height: 50px;
width:50px;
margin:10px;
background-color: #FFF;
}
.red {
background-color: #F00;
}
<div class="square red"></div>
<div class="square red"></div>
<div class="square red"></div>
<div class="square red"></div>