【发布时间】:2015-11-29 01:35:42
【问题描述】:
我正在尝试运行一个包含动画的 while 循环。我想要发生的是让while循环暂停,让动画完成,然后继续。
这不是我的实际代码,但我相信它解决了问题:
var counter = 0;
while (counter < 2) {
$(".one").animate({"left":"+=50px"}, "slow", function() {
counter++;
});
};
这会使我的浏览器崩溃,因为它在继续执行 while 循环之前不等待动画完成(因此它不等待计数器增加)。提前致谢!
https://jsfiddle.net/uhmctey6/
编辑
感谢大家解释为什么这是不可能的。但是,我仍然不确定如何做我需要做的事情,而且由于我没有使用我的实际代码作为示例,我不确定建议的解决方案是否有帮助。
这就是我真正想做的事情:我正在制作一个带有阅读器和它读取的单元阵列的图灵机。运行此功能后,我想搜索图灵代码行列表,以查看其中是否与阅读器当前状态和阅读器正在扫描的当前单元格的内容相匹配。如果匹配,我希望读者根据相关的图灵代码行进行一系列更改,然后在视觉上移动到下一个单元格,并且只有在此动画完成后 em> 重新开始这个过程,通过搜索图灵代码行列表来查看是否有匹配阅读器的新状态等。
我知道这不能像我拥有的那样使用while循环来实现,但是有没有办法以另一种方式做这样的事情?非常感谢!
var run_program = function() {
while (true) {
for (var j=0; j< program.length; j++) { //loops through the different lines of turingcode
if (reader.state === program[j].state && reader.scanning === program[j].scanning) { //if there is a line of turingcode for the readers current state and scanning values.
cellArray[reader.location].content = program[j].print; //change the content of the current cell to the new value
reader.location += 1; //increase the value of the reader's location
$(".reader").animate({"left":"+=50px"}, "slow"); //move the div to the right so it appears to be under the next cell
reader.scanning = cellArray[reader.location].content; //update the readers scanning value to be the value of the new cell
reader.state = program[j].next_state; // update the state of the reader to that specified by the line of turingcode
break;
}
else if (j === $scope.program.length-1) { //if there is no line of turingcode for the readers current state and scanning values, and j has looped through the entire list of turingcode lines
return; //halt the function
}
}
}
}
【问题讨论】:
-
您到底想做什么?您是否尝试在每个动画步骤中更新计数器值?还是要多次运行同一个动画?
-
不,不是。 JavaScript 的评估绑定到单个线程。
while循环将无限期地保持线程忙碌,防止执行任何其他事件,包括.animate()回调。 -
这不是我的实际代码,它只是一个简化的例子。但我想在这个例子中,我希望动画运行两次,每次动画完成后增加计数器。
标签: javascript jquery