我有一些代码,我有一个计时器。背景每 3 秒改变一次颜色,当您将鼠标悬停在停止按钮上时,颜色更改器会暂停,我有一个 onclick 事件,我正在与 mouseout 事件结合使用,但 mouseout 事件取消了我的 onclick 事件。在我将鼠标从停止按钮移动后,我该怎么做才能使 onclic 事件仍然有效?
代码:jsfiddle
<script>
var colors = new Array();
colors[0] = "green";
colors[1] = "blue";
colors[2] = "gray";
var i = 0;
var timer;
function changeOfPlans() {
timer = setInterval("colorChange()", 3000);
}
function colorChange() {
document.getElementById("one").style.backgroundColor = colors[i];
document.getElementById("two").style.backgroundColor = colors[i];
i++;
if (i == 3 || i > 3) {
//start over by setting i to 0
i = 0;
}
}
function stop() {
clearTimeout(timer);
}
</script>