简体   繁体   English

每个循环完成后运行一个jquery函数

[英]run a jquery function after each loop completes

I have a situation where i am running a jquery function and in that function i have an each loop. 我有一种情况,我正在运行一个jQuery函数,在该函数中,我有一个each循环。 The each loop takes some time for processing and that is why the next statement executes before each completes. 每个循环都需要花费一些时间来处理,这就是为什么下一个语句要在每个语句完成之前执行。 This creates a problem for me. 这给我带来了一个问题。 I want a function to execute when each completes. 我希望每个完成时执行一个函数。 My function is as follows:- 我的功能如下:

function myFunc() {
// Do something

$.each(mylist, function (i, val) {
    // do something
    filepicker.store(myList, function (stored_fpfile) {
                    console.log("Store successful:", JSON.stringify(stored_fpfile));
                }, function (FPError) {
                   // Error
                }, function (progress) {
                    console.log("Loading: " + progress + "%");
                }
                );
});

CallMyFunction();
}

Call my function executes before each loop finishes. 在每个循环完成之前调用我的函数执行。

I dont want to use count of the list to detect and run my procedure. 我不想使用列表计数来检测和运行我的程序。 I want a reliable solution. 我想要一个可靠的解决方案。

I am using the InkFilePicker API to store files to Amazon 我正在使用InkFilePicker API将文件存储到Amazon

Any help is appreciated. 任何帮助表示赞赏。

I would suggest generating deferred objects for each iteration that are then stored in an array. 我建议为每次迭代生成延迟的对象,然后将其存储在数组中。 Then, after the each, you can wait until all those deferred objects are complete to run your other function. 然后,在每个对象之后,您可以等到所有这些延迟的对象都完成后再运行其他功能。

function myFunc() {
    // Do something
    var promiseArray = $.map(mylist, function (i, val) {
        return $.Deferred(function(def){
            // do something
            filepicker.store(val, function (stored_fpfile) {
                def.resolve(stored_fpfile);
            }, function (FPError) {
                def.reject(FPError);
            }, function (progress) {
                def.notify(progress); // won't actually be accurate
            });        
        }).promise();
    });
    $.when.apply($,promiseArray).done(function(){
        console.log(arguments); // array of results from onSuccess callbacks
        CallMyFunction();
    })

}

Apparently, the store is a wrapper around an AJAX object. 显然,商店是围绕AJAX对象的包装。 You should test the progress and when it is completed call CallMyFunction, like so before the call to each: 您应该测试进度,并在完成时调用CallMyFunction,就像在调用每个函数之前一样:

items = myList.length;

And inside each: 在每个里面:

...
function (progress) {
    console.log("Loading: " + progress + "%");
    if (--items === 0)
    {
        CallMyFunction();
    }
}
...

There aren't any counter arguments, performance is not impacted by this, you're sending data over the Internet which is the real bottleneck. 没有任何反驳的参数,性能不受此影响,您正在通过Internet发送数据,这是真正的瓶颈。

This is also reliable. 这也是可靠的。 In case of error you should decrement with --items too. 如果发生错误,您也应该使用--items递减。

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

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