Javascript jQuery ajax,等到 beforeSend 动画完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5212949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax, wait until beforeSend animation finishes
提问by James
HTML & CSS:
HTML 和 CSS:
<a href="#">link</a>
<img src="http://2.bp.blogspot.com/_OIHTbzY7a8I/TOaiTKLqszI/AAAAAAAAAHM/eb3iiOqxzKg/s640/Auto_Audi_Audi_concept_car_005130_.jpg" />
<div></div>
img { display: none; }
a { display: block; }
JS:
JS:
$("a").click(function(){
$.ajax({
url: "test.php",
contentType: "html",
beforeSend: function(){
$("img").fadeIn(600, function(){
$("div").append(" | beforeSend finished | ");
});
},
error: function(){
$("div").append(" | error | ");
}
});
return false
});
The problem is, error
function starts, before animation in beforeSend
function finishes.
问题是,error
函数开始之前,函数中的动画beforeSend
完成。
Here is working example http://jsfiddle.net/H4Jtk/2/
这是工作示例http://jsfiddle.net/H4Jtk/2/
error
should begin to work only when beforeSend
is finished. How to do this?
error
应该在beforeSend
完成后才开始工作。这该怎么做?
I use beforeSend
function to start animation of the blocks when ajax starts. I can't remove it.
beforeSend
当ajax启动时,我使用函数来启动块的动画。我无法删除它。
Thanks.
谢谢。
回答by eyelidlessness
You can use a custom event to wait until the animation is complete, like this:
您可以使用自定义事件等待动画完成,如下所示:
$("a").click(function(){
var img = $("img"),
div = $("div");
$.ajax({
url: "test.php",
contentType: "html",
beforeSend: function(){
img.fadeIn(600, function(){
div.append(" | beforeSend finished | ");
div.trigger("animationComplete");
});
},
error: function(){
if(img.is(':animated')) {
div.one("animationComplete", function() {
div.append(" | error | ");
});
} else {
div.append(" | error | ");
}
}
});
return false
});
Notes:
笔记:
jQuery.one()
attaches an event handler that fires once and is then removed.jQuery.is(':animated')
is a custom selector provided by jQuery to determine if an element is animated using jQuery's built-in animation methods (egfadeIn
).
jQuery.one()
附加一个事件处理程序,该事件处理程序触发一次然后被删除。jQuery.is(':animated')
是 jQuery 提供的自定义选择器,用于确定元素是否使用 jQuery 的内置动画方法(例如fadeIn
)进行动画处理。
New jsFiddle: http://jsfiddle.net/pgjHx/
新的 jsFiddle:http: //jsfiddle.net/pgjHx/
In the comments, Happy has asked how to handle multiple animations. One way would be to add a condition to the animationComplete
event handler to see if animation is ongoing. For example:
在评论中,Happy 询问了如何处理多个动画。一种方法是向animationComplete
事件处理程序添加条件以查看动画是否正在进行。例如:
$("a").click(function(){
var img = $("img"),
div = $("div");
$.ajax({
url: "test.php",
contentType: "html",
beforeSend: function(){
img.fadeIn(600, function(){
div.append(" | beforeSend finished | ");
div.trigger("animationComplete");
});
// Add another animation just to demonstrate waiting for more than one animation
img.animate({
marginTop: '-=5',
opacity: '-=.5'
}, 700, function() {
div.trigger("animationComplete");
});
},
error: function(){
if(img.is(":animated")) {
// Note I've switched to bind, because it shouldn't be unbound
// until all animations complete
div.bind("animationComplete", function() {
if(img.is(":animated")) {
return;
}
div.append(" | error | ");
div.unbind("animationComplete");
});
} else {
div.append(" | error | ");
}
}
});
return false
});
回答by Praveen Prasad
you can do this
你可以这样做
//run your animation on link click, and when animation gets completed, // then start your ajax request.
//在链接点击时运行你的动画,当动画完成时,//然后开始你的ajax请求。
$("a").click(function () {
$("img").fadeIn(600, function () {
$("div").append(" | beforeSend finished | ");
$.ajax({
url: "test.php",
contentType: "html",
error: function () {
$("div").append(" | error | ");
}
});
return false
});
});
});
回答by Pointy
The thing is that "beforeSend" isfinished - it will exit long before that "fadeIn" finishes. I don't know what you're trying to accomplish so it's hard to offer a solution. You'd have to wait to start the ajax operation until afterthe animation sequence (the "fadeIn") completes if you wanted to guarantee that to happen first.
问题是,“beforeSend”是成品-长则退出前的“淡入”结束。我不知道您要完成什么,因此很难提供解决方案。如果您想确保首先发生,则必须等到动画序列(“淡入淡出”)完成后才能开始 ajax 操作。