简体   繁体   English

fadeIn回调无法从jquery.ajax内部成功进行

[英]fadeIn callback doesn't work from inside jquery.ajax success

I get some data using AJAX, prepend element to body and then display it. 我使用AJAX获取了一些数据,将元素添加到body ,然后显示出来。 After it's displayed I need to perform some client-side operations on a new element (say, I need to render Latex using codecogs' script ). 显示它之后,我需要在一个新元素上执行一些客户端操作(例如,我需要使用codecogs的脚本渲染Latex)。 My code looks like this: 我的代码如下所示:

$.ajax({
/* ... */
success: function(data){
    /* new element generation... */
    $(newelement).fadeIn(100, LatexIT.render('*'));
},
/* ... */ });

As you can see I call LatexIT.render('*') as a callback from fadeIn. 如您所见,我将LatexIT.render('*')称为fadeIn的回调。 It should perform whatever LatexIT.render('*') does right after the animation is over. 动画结束后,它应该执行LatexIT.render('*')所做的任何LatexIT.render('*') But when called from $.ajax success the callback doesn't work, although fade itself occures normally. 但是,从$.ajax success调用时,回调不起作用,尽管淡入淡出本身通常会发生。

UPDATE : I tried to replace LatexIT.render('*') with any simple function but it doesn't work. 更新 :我试图用任何简单的函数替换LatexIT.render('*') ,但不起作用。 And fadeIn(100, function () { LatexIT.render('*') }); fadeIn(100, function () { LatexIT.render('*') }); does work when called from outside of ajax success. 从ajax成功外部调用时确实起作用。

LatexID.render('*') is the syntax to call the .render method rather than bind it. LatexID.render('*')调用 .render方法而不是绑定它的语法。 Unless that itself returns a function, which is unlikely, you need to use this syntax: 除非它本身返回一个函数(这是不可能的),否则您需要使用以下语法:

.fadeIn(100, function () { LatexIT.render('*') });

You could also do: 您也可以这样做:

.fadeIn(100, LatexIT.render.bind(undefined, '*'))

assuming the browsers you need to support have .bind 假设您需要支持的浏览器具有.bind

There you are not giving it a success callback, you are executing your render and giving what the render method returns to the complete argument of the fade animation, which is actually not a function that the animation can call at the end. 在这里,您没有给它提供成功回调,而是在执行渲染,并将render方法返回的内容返回给淡入淡出动画的完整参数,实际上这不是动画最后可以调用的函数。

You should just wrap it into an anonymous function: $(newElement).fadeIn(100, function() { LatexIT.render("*"); }) 您应该将其包装到一个匿名函数中: $(newElement).fadeIn(100, function() { LatexIT.render("*"); })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM