如何突破Jquery的每个循环

时间:2013-12-17 17:27:08

标签: jquery asynchronous jquery-deferred

我的问题是:How to break out of jQuery each Loop

我需要退出jquery $ .each(),但在这种情况下我不能返回FALSE,因为退出条件是触发一个将解决延迟的函数。所以我需要退出每个,但有承诺。

function KillLayer(id) {
    var defer = $.Deferred();
    $.each(vm.get("DeviceData"), function (idx, item) {
        if (item.Type == id) {
            vm.DeviceData.splice(idx, 1); // remove the item from the list
            removeLayer(defer, id)  // delete it from the PouchDB (IDB) database (async)
            return defer.promise();
        }
    });
    defer.resolve(); // layer was not found, so just resolve & return
    return defer.promise();
}

4 个答案:

答案 0 :(得分:1)

尝试这样。

objDP = null;  // To get Deferred’s Promise object.

$.each(vm.get("DeviceData"), function (idx, item) {
    if (item.Type == id) {
        vm.DeviceData.splice(idx, 1); // remove the item from the list
        removeLayer(defer, id)  // delete it from the PouchDB (IDB) database (async)
        objDP = defer.promise();
        return false;
    }
});

答案 1 :(得分:1)

正如您所发现的那样,您无法从其KillLayer()循环中返回each(),因为该上下文中的return语句具有不同的含义。

以下是几种不同的方法:

1:使用布尔标志来表示是否发生了删除,并相应地处理延迟。

function KillLayer(id) {
    var defer = $.Deferred(), deleted = false;
    $.each(vm.get("DeviceData"), function (idx, item) {
        if (item.Type == id) {
            vm.DeviceData.splice(idx, 1); // remove the item from the list
            removeLayer(defer, id);  // delete it from the PouchDB (IDB) database (async)
            deleted = true;
            return false;//break from each() loop
        }
    });
    return deleted ? defer.promise() : defer().resolve().promise();//return either a promise of deletion or a ready-resolved promise.
}

2 :(如果你可以控制removeLayer())安排removeLayer()生成并返回自己的承诺,而不是传递延迟。这样您就可以按如下方式简化KillLayer()

function KillLayer(id) {
    var deletionPromise;
    $.each(vm.get("DeviceData"), function (idx, item) {
        if (item.Type == id) {
            vm.DeviceData.splice(idx, 1); // remove the item from the list
            deletionPromise = removeLayer(id);  // delete it from the PouchDB (IDB) database (async), and receive back a promise
            return false;//break from each() loop
        }
    });
    return deletionPromise || $.Deferred().resolve().promise(); //return either a promise of deletion from the DB, or a ready-resolved promise
}

虽然只有一个线差异,但我认为第二个appraoch更清晰。当removeLayer()执行异步活动时,它应该真正返回它自己的promise,而不是依赖于传递给它的Deferred。

答案 2 :(得分:0)

这不会从每个循环中断,但它会使以下迭代无效:

function KillLayer(id) {
    var defer = $.Deferred();
    var result = null;
    $.each(vm.get("DeviceData"), function (idx, item) {
        if (!result && item.Type == id) {
            vm.DeviceData.splice(idx, 1); // remove the item from the list
            removeLayer(defer, id)  // delete it from the PouchDB (IDB) database (async)
            result = defer.promise();
        }
    });
    return result || defer.resolve();
}

答案 3 :(得分:0)

我想出了Beetroots独立回答#1。首先,我意识到循环遍历$ each()并从javascript数组中删除您正在循环的项目是一种很好的错误方法。我真的应该这样做:

$.each(clone(vm.get("DeviceData")), function (idx, item)` {

其中clone函数定义为:

function clone(obj) {
    return jQuery.extend({}, obj);
}

function deepClone(obj) {
    return jQuery.extend(true, {}, obj);
}

但在我的情况下,我不需要额外的保护,所以我刚刚更新了我的解决方案:

function KillLayer(id) {
    var defer = $.Deferred();
    var found = false;
    $.each(vm.get("DeviceData"), function (idx, item) {
        if (item.Type == id) {
            vm.DeviceData.splice(idx, 1);
            Compare();
            dbManifest.LayerInfo = vm.DeviceData.toJSON();
            removeLayer(defer, id)
            found = true;
            return false;
        }
    });
    if (!found) defer.resolve(); // layer was not found, so just resolve & return
    return defer.promise();
}