所以我一直试图进行这种转换,这在我脑海中很简单。陈述变量" num"等于1开始,设置"如果"或"而"何时" num"等于2,某个div的背景颜色将从白色变为黄色,然后以0.5秒的间隔变回。我认为我的问题是我似乎无法改变全局变量。我已将其更改为2,并且循环有效,但是当它1并且我尝试使用按钮更改为2时,没有什么可以开心的!这是我到目前为止的JS:
var num = 1;
document.getElementById("numberDisplay").innerHTML = num;
if (num == 2) {
var flash = setInterval(timer, 1000);
function timer() {
document.getElementById("colorChanger").style.backgroundColor = "yellow";
setTimeout(white, 500);
function white() {
document.getElementById("colorChanger").style.backgroundColor = "white";
}
}
} else {
document.getElementById("colorChanger").style.backgroundColor = "white";
document.getElementById("numberDisplay").innerHTML = num;
}
document.getElementById("buttonOne").onclick=function(){num = 1;}
document.getElementById("buttonTwo").onclick=function(){num = 2;}
编辑:1/30/2018 12:25 pm 真的很感谢帮帮人!我的一个朋友实际上给了我一些建议,那就是使用window.onload和事件计时器!这就是我想出的:
var num = 1;
window.onload = function(){
var button1 = document.getElementById("buttonOne")
var button2 = document.getElementById("buttonTwo")
button1.addEventListener("click",function(){
num = 2;
})
button2.addEventListener("click",function(){
num = 1;
})
setInterval(checkNum, 1000);
}
function checkNum() {
if (num == 2) {
document.getElementById("buttonOne").style.backgroundColor = "#ccccb3";
document.getElementById("buttonTwo").style.backgroundColor = "white";
document.getElementById("lightChanger").style.backgroundColor = "yellow";
setTimeout(grey, 500);
function grey() {
document.getElementById("lightChanger").style.backgroundColor = "grey";
}
} else {
document.getElementById("lightChanger").style.backgroundColor = "grey";
document.getElementById("buttonOne").style.backgroundColor = "white";
document.getElementById("buttonTwo").style.backgroundColor = "#ccccb3";
}
}
答案 0 :(得分:0)
您可以直接在点击处理程序中使用timer
函数,而不是setInterval,而您的timer
函数可以使用
var colorChanger = document.getElementById("colorChanger");
function white() {
colorChanger.style.backgroundColor = "white";
}
function timer(n) {
num = n;
if (num == 2) {
colorChanger.style.backgroundColor = "yellow";
setTimeout(white, 500);
return;
}
colorChanger.style.backgroundColor = "white";
}
试试这个,
var num = 1;
var colorChanger = document.getElementById("colorChanger");
function white() {
colorChanger.style.backgroundColor = "white";
}
function timer(n) {
num = n;
if (num == 2) {
colorChanger.style.backgroundColor = "yellow";
setTimeout(white, 500);
return;
}
colorChanger.style.backgroundColor = "white";
}
document.getElementById("buttonOne").onclick = function() {
timer(1);
}
document.getElementById("buttonTwo").onclick = function() {
timer(2);
}

<div id="colorChanger">switch</div>
<button id='buttonOne'>1</button>
<button id='buttonTwo'>2</button>
&#13;
答案 1 :(得分:0)
但是当它为1并且我尝试使用按钮更改为2时,没有什么可以开心的!
因为在点击时,您正在更改变量。除非可以观察到,否则不会触发任何事情。您必须将相关代码包装为函数并改为调用它。
function timer(n) {
function updateBackground(color) {
document.getElementById("colorChanger").style.backgroundColor = "yellow";
}
var colors = ['white', 'yellow'];
updateBackground(colors[n - 1]);
if (n === 2) {
setTimeout(function() {
updateBackground('white')
}, 500);
}
}
document.getElementById("buttonOne").onclick = function() {
timer(1);
}
document.getElementById("buttonTwo").onclick = function() {
timer(2);
}
<div id="colorChanger">switch</div>
<button id='buttonOne'>1</button>
<button id='buttonTwo'>2</button>
几点改进,
.onclick
。如果我获取元素并将其替换为我的函数,它将破坏您的代码。始终更喜欢使用.addEventListener
。但这是自以为是。
function timer(n) {
if (n === 2) {
document.getElementById("colorChanger").classList.add("yellow");
setTimeout(function() {
document.getElementById("colorChanger").classList.remove("yellow");
}, 500);
}
}
document.getElementById("buttonOne").onclick = function() {
timer(1);
}
document.getElementById("buttonTwo").onclick = function() {
timer(2);
}
.white {
background: white;
}
.yellow {
background: yellow;
/* This is for demo purpose only */
font-weight: bold;
}
<div id="colorChanger" class='white'>switch</div>
<button id='buttonOne'>1</button>
<button id='buttonTwo'>2</button>
答案 2 :(得分:0)
我改变了一些事情:
(function(){
var changeColor = function(num) {
document.getElementById("numberDisplay").innerHTML = num;
if (num === 2) {
var flash = setInterval(timer, 1000);
function timer() {
document.getElementById("colorChanger").style.backgroundColor = "yellow";
setTimeout(white, 500);
function white() {
document.getElementById("colorChanger").style.backgroundColor = "white";
}
}
} else {
document.getElementById("colorChanger").style.backgroundColor = "white";
document.getElementById("numberDisplay").innerHTML = num;
}
}
document.getElementById("buttonOne").addEventListener('click', function(evt){
let num = 1;
changeColor(num);
});
document.getElementById("buttonTwo").addEventListener('click', function(evt){
let num = 2;
changeColor(num);
});
})();
<div id="numberDisplay"></div>
<div id="colorChanger">Color</div>
<button id="buttonOne">1</button>
<button id="buttonTwo">2</button>