我想在我的 while Colors(interval) 触发时禁用我的下拉菜单。就我而言,我在 5 秒后手动设置了截止时间。我遇到的是,当我将重新激活器放在我的案例块中时,它不会等待 setTimeout。还是两个调用同时触发,所以当 setTimeout 触发(也就是等待那五秒)时,下一个调用(重新激活器)也会触发?
另一个问题——也是我想在我的颜色触发时停用下拉菜单的原因——是我注意到当颜色触发时,如果我再次点击下拉菜单——也就是再次触发对它的调用,第二个调用将导致颜色无休止地触发(假设以某种方式创建了无限循环)。思考为什么?
function timeToHexColor(){
var time = new Date();
var timestamp = time.toString("hh:mm:ss");
document.getElementById("board").innerHTML +=
"#" + timestamp.split(":").join("") + "<br/>";
}
function Colors(interval) {
this.interval = interval;
switch (this.interval) {
case 'second':
document.getElementById('options').disabled = true;
x = setInterval(timeToHexColor,1000);
setTimeout(stopColors, 5000);
//Placing the re-activtor here executes instantly not after the setTimeout.
//document.getElementById('options').disabled = false;
break;
case 'minute':
x = setInterval(timeToHexColor,60000);
setTimeout(stopColors, 5000);
document.getElementById('options').disabled = true;
break;
case 'hour':
x = setInterval(timeToHexColor,60000*60);
setTimeout(stopColors, 5000);
document.getElementById('options').disabled = true;
break;
case 'day':
x = setInterval(timeToHexColor,60000*1440);
setTimeout(stopColors, 5000);
document.getElementById('options').disabled = true;
break;
default:
}
}
function stopColors() {
clearInterval(x);
//Placing the re-activator here instead works they way I intended,
//after the repeat cycle is finished.
document.getElementById('options').disabled = false;
}
$("#options").prop('selectedIndex',-1);
$("#options").change(function() {
Colors('second');
});