[英]Javascript will not wait for JQuery animation to finish before executing next command
I'm trying to create a simple 2-D game in Javascript. 我正在尝试用Javascript创建一个简单的2D游戏。 I'm going to use jQuery to do some animations. 我将使用jQuery制作一些动画。
There will be buttons that a player will use to call various functions (Move up/down/left/right, Attack, Defend, etc). 玩家将使用按钮来调用各种功能(上/下/左/右移动,攻击,防御等)。
A movement() function will call a secondary function, animateCharacter(), to handle the movement of an image object on screen. Movement()函数将调用辅助函数animateCharacter()来处理图像对象在屏幕上的移动。
The problem I'm having is that the next command in the movement() function executes before the animateCharacter() function has finished. 我遇到的问题是在animateCharacter()函数完成之前,先执行了Movement()函数中的下一个命令。
I tried to add a callback function, but that didn't fix the situation. 我试图添加一个回调函数,但是并不能解决这种情况。 I've tried many other things -- setInterval, setTimeout, .delay, etc. Nothing seems to fix this situation. 我尝试了许多其他操作-setInterval,setTimeout,.delay等。似乎没有任何方法可以解决这种情况。 What am I not doing, or what am I doing wrong? 我在做什么,或者我在做什么错?
Here's a simplified example of the problem.... 这是问题的简化示例。
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <style> #mainContainer { border: 1px solid gray; width:100px; height:100px; position: relative; } #myObject { border: 1px solid gray; width: 30px; height: 30px; color:blue; text-align:center; background-color:yellow; position:relative; } </style> </head> <body> <div id="mainContainer"> <div id="myObject">:c)</div> </div> <script> // MOVE THE BLOCK TO THE RIGHT function animateCharacter(callbackSent) { $("#myObject").animate({left: "+=50"},1500, function () { callbackSent();}); } // DO THIS WHEN THE BUTTON IS PUSHED function doTask() { myCallback = function () { document.getElementById("mainContainer").innerHTML = "hello"; }; animateCharacter(myCallback); // WORKS FINE IF THIS IS COMMENTED OUT, // BUT I WANT MORE CODE TO EXECUTE document.getElementById("mainContainer").innerHTML = "goodbye"; } </script> <button id="pushMe" onclick="doTask();">Move the Block</button> </body> </html>
Try This. 尝试这个。
Moved document.getElementById("mainContainer").innerHTML = "goodbye";
已移动document.getElementById("mainContainer").innerHTML = "goodbye";
to the setTimeout function, that will execute after Hello is printed in the div. 到setTimeout函数,它将在div中打印Hello后执行。
Your code was executing like: 您的代码执行如下:
// MOVE THE BLOCK TO THE RIGHT function animateCharacter(callbackSent) { $("#myObject").animate({left: "+=50"},1500, callbackSent); } // DO THIS WHEN THE BUTTON IS PUSHED function doTask() { myCallback = function () { document.getElementById("mainContainer").innerHTML = "hello"; setTimeout(function(){document.getElementById("mainContainer").innerHTML = "goodbye"; },1000); }; animateCharacter(myCallback); // WORKS FINE IF THIS IS COMMENTED OUT, // BUT I WANT MORE CODE TO EXECUTE }
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <style> #mainContainer { border: 1px solid gray; width:100px; height:100px; position: relative; } #myObject { border: 1px solid gray; width: 30px; height: 30px; color:blue; text-align:center; background-color:yellow; position:relative; } </style> </head> <body> <div id="mainContainer"> <div id="myObject">:c)</div> </div> <button id="pushMe" onclick="doTask();">Move the Block</button> </body> </html>
Your code does what it is told at the moment. 您的代码会执行目前的指示。 You need to introduce a delay before showing goodbye. 您需要稍稍延迟才能告别。
// MOVE THE BLOCK TO THE RIGHT
function animateCharacter(callbackSent) {
$("#myObject").animate({
left: "+=50"
}, 1500, callbackSent);
}
// DO THIS WHEN THE BUTTON IS PUSHED
function doTask() {
animateCharacter(function () {
$("#mainContainer").html("hello");
setTimeout(function () {
$("#mainContainer").html("Goodbye");
}, 3000);
});
}
$('#pushMe').click(doTask);
JSFiddle: http://jsfiddle.net/TrueBlueAussie/7tucg2s5/1/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/7tucg2s5/1/
onclick
handler to the jQuery equivalent as that is easier to maintain. 我将您的内联onclick
处理程序移至jQuery等效项,因为它更易于维护。 You can't put your "goodbye" message directly in #myContainer because this will destroy #myObject. 您不能将“再见”消息直接放入#myContainer中,因为这会破坏#myObject。 Add another div to hold the message: 添加另一个div来保存消息:
HTML: HTML:
<div id="mainContainer">
<div id="myObject">:c)</div>
<div id="myText"></div>
</div>
CSS: CSS:
#myText {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
JavaScript: JavaScript的:
function doTask() {
myCallback = function () { document.getElementById("mainContainer").innerHTML = "hello"; };
animateCharacter(myCallback);
// WORKS FINE IF THIS IS COMMENTED OUT,
// BUT I WANT MORE CODE TO EXECUTE
document.getElementById("myText").innerHTML = "goodbye";
}
Fiddle here: http://jsfiddle.net/robbyn/0qkt0v5r/ 在这里提琴: http : //jsfiddle.net/robbyn/0qkt0v5r/
Try 尝试
function animateCharacter() { return $("#myObject").animate({ left: "+=50" }, 1500, function() { $(this) .fadeOut(500) .parent() .html("<span>hello</span>") .find("span") .delay(1500, "fx") .fadeOut(250, function() { $(this).html("Goodbye").fadeIn(250) }) }) } $("#pushMe").on("click", function() { animateCharacter() });
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <style> #mainContainer { border: 1px solid gray; width: 100px; height: 100px; position: relative; } #myObject { border: 1px solid gray; width: 30px; height: 30px; color: blue; text-align: center; background-color: yellow; position: relative; } </style> </head> <body> <div id="mainContainer"> <div id="myObject">:c)</div> </div> <button id="pushMe">Move the Block</button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.