0
이 기능 중 하나만 실행하면됩니다. 다른 함수를 계속 실행하면서 함수 실행을 중단 할 수 있습니까?하나의 기능을 다른 모든 기능을 중지시키는 방법이 있습니까?
예를 들어 "do_move_right()"함수가 현재 실행중인 경우 함수를 실행 중지시키는 버튼을 눌러 "do_move_left()"함수를 시작하려고합니다.
CODE :
function do_move_right() {
sheepr = document.getElementById('sb');
mover = sheepr.style.left;
mover = parseInt(mover.substring(0,mover.length-2));
mover += 10;
sheepr.style.left = (mover)+'px';
if (mover < 940) {
window.setTimeout(function() {
do_move_right('sb');
}, 250);
}
}
function do_move_left() {
sheepl = document.getElementById('sb');
movel = sheepl.style.left;
movel = parseInt(movel.substring(0,movel.length-2));
movel -= 10;
sheepl.style.left = (movel)+'px';
if (movel < 940) {
window.setTimeout(function() {
do_move_left('sb');
}, 250);
}
}
function do_move_up() {
sheepu = document.getElementById('sb');
moveu = sheepu.style.top;
moveu = parseInt(moveu.substring(0,moveu.length-2));
moveu -= 10;
sheepu.style.top = (moveu)+'px';
if (moveu < 940) {
window.setTimeout(function() {
do_move_up('sb');
}, 250);
}
}
function do_move_down() {
sheepd = document.getElementById('sb');
moved = sheepd.style.top;
moved = parseInt(moved.substring(0,moved.length-2));
moved += 10;
sheepd.style.top = (moved)+'px';
if (moved < 940) {
window.setTimeout(function() {
do_move_down('sb');
}, 250);
}
}
실제로 내부 함수 내용을 if 문으로 래핑하여 테스트가 통과하든 실패하든 if 문 닫는 중괄호 바로 뒤에 setTimeout을 수행 할 수 있습니다. –