Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法让一个函数停止所有其他函数?_Javascript_Execute - Fatal编程技术网

Javascript 有没有办法让一个函数停止所有其他函数?

Javascript 有没有办法让一个函数停止所有其他函数?,javascript,execute,Javascript,Execute,我希望一次只运行其中一个函数。是否有一种方法可以在继续执行另一个函数时停止执行该函数 例如,如果函数“do_move_right()”当前正在执行,我希望能够按下一个按钮,使函数停止执行并开始执行函数“do_move_left()” 代码: 函数do\u move\u right(){ sheepr=document.getElementById('sb'); mover=sheepr.style.left; mover=parseInt(mover.substring(0,mover.leng

我希望一次只运行其中一个函数。是否有一种方法可以在继续执行另一个函数时停止执行该函数

例如,如果函数“do_move_right()”当前正在执行,我希望能够按下一个按钮,使函数停止执行并开始执行函数“do_move_left()”

代码:

函数do\u move\u right(){
sheepr=document.getElementById('sb');
mover=sheepr.style.left;
mover=parseInt(mover.substring(0,mover.length-2));
移动器+=10;
sheepr.style.left=(移动器)+'px';
如果(移动器<940){
setTimeout(函数(){
你向右移动(“某人”);
}, 250);
}
}
函数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';
如果(移动<940){
setTimeout(函数(){
你向左移动(“某人”);
}, 250);
}
}
函数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';
如果(移动u<940){
setTimeout(函数(){
向上移动(“某人”);
}, 250);
}
}
函数do_move_down(){
sheepd=document.getElementById('sb');
移动=sheepd.style.top;
moved=parseInt(moved.substring(0,moved.length-2));
移动+=10;
sheepd.style.top=(移动)+'px';
如果(移动<940){
setTimeout(函数(){
你是否向下移动(“某人”);
}, 250);
}
}

JavaScript不能同时运行两个函数。一旦一个函数被输入,它将继续执行直到返回。除非函数本身提前返回(如使用if语句检查提前退出条件),否则无法中断它

因此,您可能要寻找的是一个变量,您可以动态设置该变量,函数将检查该变量是否允许它们执行其操作。比如:

var canMoveLeft = true;

在执行任何操作之前,只需在do_move_left()函数中检查该变量,如果测试失败,则尽早返回。

实际上,您可能希望使用if语句包装内部函数内容,以便无论测试通过还是失败,都可以在if语句的右括号结束后执行setTimeout。
var canMoveLeft = true;