jQuery .each() 完成后回调函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23822722/
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
callback to function after .each() has completed?
提问by user756659
Not sure if this is possible or how to go about it. I am using the following in an $.ajax response which works perfectly fine, however, I need to call the function loadSlider();
AFTER the loop is finished iterating.
不知道这是否可能或如何去做。我在 $.ajax 响应中使用以下内容,它工作得非常好,但是,我需要loadSlider();
在循环完成迭代后调用该函数。
if (response.success)
{
$.each( response.screenshots, function( index, value ) {
//add the slide
$( '#slider1' ).prepend( '<li data-id="'+value.screenshot_id+'"><img src="/showimage.php?show='+value.image_filename+'" alt=""></li>' );
//add the pager
$( '#rslides-pager' ).prepend( '<li><a href="javascript:;"># here</a></li>' );
});
//want to call loadSlider(); AFTER everything above is completed
}
回答by ThiefMaster
$.each()
iterates over the array synchronously, so you can simply run your code after the $.each();
call.
$.each()
同步迭代数组,因此您可以在$.each();
调用后简单地运行代码。
if (response.success) {
$.each( response.screenshots, function( index, value ) {
// do stuff
});
loadSlider();
}
As you mention AJAX: Only the actual success callback (i.e. the function that most likely contains the code you posted) is executed asynchronously. Any code inside that function runs synchronously (unless you call another asynchronous function in there of course).
正如您提到的 AJAX:只有实际的成功回调(即最有可能包含您发布的代码的函数)是异步执行的。该函数中的任何代码都同步运行(当然,除非您在其中调用另一个异步函数)。
回答by Johan Morales
jquery has a jQuery.when(), this provides a way to execute callback functions based on zero or more objects, usually Deferred objects that represent asynchronous events.
jquery 有一个 jQuery.when(),它提供了一种基于零个或多个对象执行回调函数的方法,通常是表示异步事件的延迟对象。
Now in your code maybe:
现在在你的代码中可能:
if (response.success) {
var iterate = $.each( response.screenshots, function( index, value ) {
// do stuff
});
$.when(iterate)
.then( loadSlider, myFailure );
}
The first function is on success callback and the other function is on fail callback.
第一个函数是成功回调,另一个函数是失败回调。
回答by ValeroLlinares
You can launch slider in the last iteration, it's easy:
您可以在最后一次迭代中启动滑块,这很简单:
if(response.success)
{
var count = response.screenshots.length;
$.each(response.screenshots, function (index, value) {
//add the slide
$('#slider1').prepend('<li data-id="' + value.screenshot_id + '"><img src="/showimage.php?show=' + value.image_filename + '" alt=""></li>');
//add the pager
$('#rslides-pager').prepend('<li><a href="javascript:;"># here</a></li>');
if (!--count) {
//want to call loadslider(); after everything above is completed
loadslider();
}
});
}
回答by Ahmad Aghazadeh
$.each( response.screenshots, function( index, value ) {
}).promise().done(function () {
//Completed
});
Sample :
样本 :
if (response.success)
{
$.each( response.screenshots, function( index, value ) {
//add the slide
$( '#slider1' ).prepend( '<li data-id="'+value.screenshot_id+'"><img src="/showimage.php?show='+value.image_filename+'" alt=""></li>' );
//add the pager
$( '#rslides-pager' ).prepend( '<li><a href="javascript:;"># here</a></li>' );
}).promise().done(function () {
//Completed
});
}