setInterval 메서드를 처리하는 자동 단추 (자동)를 두 번 클릭하면 색상 조각이 빠르게 움직입니다. 이제 내가 알기로 여기에 이유가 있습니다. 이 jsfiddle DEMO OF COLOR DIVS WITH SETINTERVAL METHOD이벤트를 두 번 이상 처리하는 버튼을 클릭하면 setInterval이 이상하게 들립니까?
몸의 데모입니다 :
<div id="placeDiv1">ok</div>
<button id="b1" onclick="forward()">Forward</button>
<button id="b2" onclick="backward()">Backward</button>
<button id="b3" onclick="skip2()">skip2</button>
<button id="b4" onclick="automatic()">auto</button>
<button id="b5" onclick="stop()">stop</button>
<script>
var myArray = ["black", "yellow", "green", "red", "blue", "blue", "black", "gray"];
var myArray1 = ["yellow", "blue", "green", "red", "green", "blue", "black", "gray"];
var i = 0;
document.getElementById("placeDiv").style.backgroundColor = myArray[i];
document.getElementById("placeDiv1").style.backgroundColor = myArray1[i];
forward = function() {
if (i == myArray.length - 1) {
i = 0;
} else {
i = i + 1;
}
document.getElementById("placeDiv1").style.backgroundColor = myArray1[i];
document.getElementById("placeDiv").style.backgroundColor = myArray[i];
};
skip2 = function() {
if (i == myArray.length - 4) {
i += 2;
alert("This is the iterator " + i)
} else if (i == 7) {
i = 0
} else {
i = i + 1;
};
document.getElementById("placeDiv1").style.backgroundColor = myArray1[i];
document.getElementById("placeDiv").style.backgroundColor = myArray[i];
};
backward = function() {
if (i == 0) {
i = myArray.length - 1;
i = myArray1.length - 1;
} else {
i = i - 1;
}
document.getElementById("placeDiv1").style.backgroundColor = myArray1[i];
document.getElementById("placeDiv").style.backgroundColor = myArray[i];
//
}
automatic = function() {
var m = setInterval(function() {
if (i == myArray.length - 1) {
i = 0;
} else {
i = i + 1;
}
document.getElementById("placeDiv1").style.backgroundColor = myArray1[i];
document.getElementById("placeDiv").style.backgroundColor = myArray[i];
}, 100);
stop = function() {
clearInterval(m)
};
};
</script>
CSS :
#placeDiv {
position: absolute;
left: 0px;
width: 100px;
height: 100px;
}
#placeDiv1 {
position: absolute;
left: 100px;
width: 100px;
height: 100px;
}
#b1 {
position: absolute;
top: 100px;
left: 0px
}
#b2 {
position: absolute;
top: 100px;
left: 80px
}
#b3 {
position: absolute;
top: 100px;
left: 170px
}
#b4 {
position: absolute;
top: 100px;
left: 270px
}
#b5 {
position: absolute;
top: 100px;
left: 320px
}
이 힘 밖에
var m
를 정의 할 필요가 help http://jsfiddle.net/q32kG/1/ – anand4tech이것은 내가 원하는 것에 대한 가장 짧은 상처입니다. 나는 그것이 도움이되었다고 생각한다. – user2887761