-1

有没有可能延迟一些?我在while循环中调用一个函数。此函数调用 executeQueryAsync 必须在循环继续之前完成。当我使用警报时,我的代码可以工作,但没有它就不行。

    while (listPermsEnumerator.moveNext()) {
        enumG = groups.getEnumerator();
        var rAssignment = listPermsEnumerator.get_current();
        var member = rAssignment.get_member();
        var groupCounter = 1;
        var name = '';

        //alert(''); This alert makes code work

        while (enumG.moveNext()) {
            var group = enumG.get_current();
            var groupname = group.get_title();
            //alert(groupname);
            if (member.get_title() === groupname) {
                name = groupname;
                SP.SOD.executeOrDelayUntilScriptLoaded(function(){
                    retrieveAllUsersInGroup(groupname, groupCounter, groups);
                }, key);
            }
            groupCounter++;
        }

        roleAssignment = this.listRoleAssignments.getByPrincipalId(member.get_id());
        roleBindings = roleAssignment.get_roleDefinitionBindings();
        // in checkPermission() another executeQqueryAsync is called
        checkPermission(context, roleAssignment, roleBindings, name);
    }

    ...
    function checkPermission(context, roleAssignment, roleBindings, name) {
        this.name = name;
        context.load(roleAssignment);
        context.load(roleBindings);
        context.executeQueryAsync(Function.createDelegate(this, Bind), Function.createDelegate(this, BindFail));
    }
4

2 回答 2

1

最简单的解决方案是以反映异步操作目的的方式对方法进行编码。您似乎正在尝试解决 ExecuteQueryAsync 并尝试“使其”同步。

这是一个类似的例子——见第二个答案:(https://sharepoint.stackexchange.com/questions/95907/executequeryasync-in-for-loop)基本上你a)内联编写回调函数,b)你把成功回调中的循环。

(在线编写“成功”回调函数的好处是成功回调函数可以访问方法中的所有变量。它是一个闭包)。

于 2015-09-27T16:47:18.393 回答
0

如果您需要遍历一组异步作业,您可以执行以下操作:

var reports = [11, 12, 14, 15];
function doTheReport() {
    if (reports.length === 0) {
        alert('All reports are done now.');
        return;
    }

    var report_Id = reports.pop();
    $.ajax({
        url: "/DoTheReport",
        complete: function () {
            doTheReport();
        }
    });
};
于 2015-09-24T14:44:16.200 回答