最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to ensure jQuery $.each is finished before continuing - Stack Overflow

programmeradmin0浏览0评论

I am working on a jQuery plugin with an add_record method (see below). If you look at the function definition, there are two $.each loops that append values to plugin.payload.

Right now, everything works fine. However, what if records or options is really big? Do I need to be concerned about the $.each() not finishing before the transmit call is issued?

If so, what is the best way to address the issue?

plugin.add_record = function (records, options, callback) {
    if (typeof (options) == "function") {
        callback = options;
        options = undefined;
    }
    if (options) {
        $.each(options, function (index, value) {
            plugin.payload.append($(value));
        });
    }
    $.each(records, function (index, value) {
        plugin.payload.append($(value));
    });
    transmit('API_AddRecord', plugin.payload, 'db', function (data) {
        return typeof (callback) == "function" ? callback(data) : data;
    });
}

I am working on a jQuery plugin with an add_record method (see below). If you look at the function definition, there are two $.each loops that append values to plugin.payload.

Right now, everything works fine. However, what if records or options is really big? Do I need to be concerned about the $.each() not finishing before the transmit call is issued?

If so, what is the best way to address the issue?

plugin.add_record = function (records, options, callback) {
    if (typeof (options) == "function") {
        callback = options;
        options = undefined;
    }
    if (options) {
        $.each(options, function (index, value) {
            plugin.payload.append($(value));
        });
    }
    $.each(records, function (index, value) {
        plugin.payload.append($(value));
    });
    transmit('API_AddRecord', plugin.payload, 'db', function (data) {
        return typeof (callback) == "function" ? callback(data) : data;
    });
}
Share Improve this question edited Sep 13, 2013 at 0:36 Mark Schultheiss 34.2k12 gold badges72 silver badges112 bronze badges asked Sep 13, 2013 at 0:19 doremidoremi 15.3k31 gold badges97 silver badges152 bronze badges 1
  • possible duplicate of Is jQuery "each()" function synchronous? – showdev Commented Sep 13, 2013 at 0:21
Add a ment  | 

2 Answers 2

Reset to default 7

$.each is synchronous, so there is no worry that the calls will not plete. The only time you would run into trouble is if starting something asynchronous inside the loop (like an ajax call).

No there is no need for concern, because $.each and .append() are synchronous functions.

In synchronous execution of statements only after pleting the one statement it will move to the next one.

The only problem could e only if you are doing some async operation inside the loop like an ajax request/animation etc which you are not doing in this case

发布评论

评论列表(0)

  1. 暂无评论