[英]How do I get the code after the for-loop to not run until the for-loop has completed its interations?
How do I stop the alert function from running until after the for loop has completed all its interations? 在for循环完成所有交互之前,如何停止运行警报功能?
$(document).ready(function() {
for (i = 1; i <= 8; i++) {
$('#ponyDiv').animate({left: '200px'}, 2000);
}
alert("here");
});
Edit: when calling animate() in a loop, you can do the following: 编辑:在循环中调用animate()时,可以执行以下操作:
$(document).ready(function () {
var completed = 0;
for (i = 1; i <= 8; i++) {
$('#ponyDiv'+i).animate({
left: '200px'
}, 200, function () {
if (completed++ == 7) alert("here");
});
}
});
Use JQuery animate() 'complete' callback which will be called right after your animation has completed: 使用JQuery animate() 'complete'回调,动画完成后立即调用:
$('#ponyDiv').animate({left: '200px'}, 2000, function() {
alert('here');
});
This will alert when the loop is completed (not when the final animation is completed). 这将在循环完成时发出警报(而不是在最终动画完成时发出警报)。
$(document).ready(function() {
for (i = 1; i <= 8; i++) {
$('#ponyDiv' + i).animate({left: '200px'}, 2000);
if(i === 8){
alert("here");
}
}
});
use deferred: 推迟使用:
$.when($('[id^=pony]').animate({left:'200px'},2000))
.then(function() { alert('done'); });
it's better of course to use a class than attribute starts with on id, but you get the gist of it. 当然,使用类比以id开头的属性更好,但是您可以从中了解要点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.