JavaScript/JQuery : Reload a page AFTER two functions have completed

标签 javascript jquery

我有一个保存按钮,它调用一个函数来打开带有两个按钮的模式对话框; “保存时间表”和“取消”。 “保存时间线”按钮调用两个函数,之后页面需要重新加载。我尝试了几种不同的方法来完成这件事......

1 只是实用地调用函数:

    function genSaveTimelinesModal() {
    $("#saveTimelineDialog").dialog({
        resizable: false,
        height: 250,
        modal: true,
        buttons: {
            "Save timelines": function() {
                editSavedTimelines(); 
                saveTimelines();
                $(this).dialog("close");
                location.reload();
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
}

2 设置回调:

function genSaveTimelinesModal() {
    $("#saveTimelineDialog").dialog({
        resizable: false,
        height: 250,
        modal: true,
        buttons: {
            "Save timelines": function() {
                editSavedTimelines(saveTimelines()); 

                location.reload();
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
}

3 使用 JQuery $.when().do():

    function genSaveTimelinesModal() {
    $("#saveTimelineDialog").dialog({
        resizable: false,
        height: 250,
        modal: true,
        buttons: {
            "Save timelines": function() {
                $.when(editSavedTimelines(), saveTimelines()).do(location.reload());
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
}

在所有三次尝试中,我的问题都是在单击“保存时间线”按钮时出现的......页面重新加载并且没有运行任何功能。当我从每个示例中提取 location.reload() 调用时,函数会按照我的需要运行。

有没有办法仅在功能完成后重新加载页面?

作为引用,以下是我调用的函数:

function saveTimelines() { 
    console.log("start save");
    for (i=1; i < timelineIndex + 1; i++) {
        var dow = startdow;
        var clientValue = $("#clientNameSelect" + i).val();
        var projectValue = $("#projectSelect" + i).val();
        var taskValue = $("#taskSelect" + i).val();
        var billingValue = $("#billingSelect" + i).val();
        var activityValue = $("#activitySelect" + i).val();
        var stateValue = $("#states" + i).val();
        var sundayValue = $("#sun" + i).val();
        var mondayValue = $("#mon" + i).val();
        var tuesdayValue = $("#tue" + i).val();
        var wednesdayValue = $("#wed" + i).val();
        var thursdayValue = $("#thu" + i).val();
        var fridayValue = $("#fri" + i).val();
        var saturdayValue = $("#sat" + i).val();



        $.ajax({
            type: "GET",
            url:"biqqyzqyr?act=API_DoQuery&query={'6'.EX.'" + projectValue + "'}AND{'16'.TV.'" + currUserEmail + "'}&clist=3&includeRids=1&fmt=structured",
            dataType: "xml",
            success: function (xml) {
                $(xml).find("record").each(function () {
                    var resourceMap = new Array();
                    $(this).children().each(function () {
                        var name = $(this).attr("id");
                        var value = $(this).text();
                        resourceMap[name] = value;
                    });
                    resourceRecords.push(resourceMap);
                });
                console.log("hi");
                var resourceRId = '3';
                for (var j = 0; j < resourceRecords.length; j++) {
                    resourceOptions = resourceRecords[j][resourceRId];
                    console.log(resourceOptions);
                }

                $.ajax({
                    type: "GET",
                    url: "biha4iayz?act=API_AddRecord&_fid_12=" + dow + "&_fid_36=" + clientValue + "&_fid_9=" + projectValue + "&_fid_7=" + taskValue + "&_fid_10=" + billingValue + "&_fid_15=" + activityValue + "&_fid_11=" + stateValue + "&_fid_13=" + sundayValue + "&_fid_57=" + mondayValue + "&_fid_58=" + tuesdayValue + "&_fid_59=" + wednesdayValue + "&_fid_60=" + thursdayValue + "&_fid_61=" + fridayValue + "&_fid_62=" + saturdayValue + "&_fid_17=" + resourceOptions,
                    dataType: "xml",
                    success: function () {
                        console.log(i+ "new")
                    },
                    fail: loadFail
                });

            },
            fail: loadFail
        });
    }

    alert(timelineIndex+savedTimelineIndex+" timelines have been saved to the system...");

}



function editSavedTimelines(callback) {
    console.log("start edit");
    for (j=1; j < savedTimelineIndex + 1; j++) {
        var dow = startdow;
        var savedRId = $("#recordsaved" + j).val();
        var sundayValue = $("#sunsaved" + j).val();
        var mondayValue = $("#monsaved" + j).val();
        var tuesdayValue = $("#tuesaved" + j).val();
        var wednesdayValue = $("#wedsaved" + j).val();
        var thursdayValue = $("#thusaved" + j).val();
        var fridayValue = $("#frisaved" + j).val();
        var saturdayValue = $("#satsaved" + j).val();

        console.log(savedRId);



     $.ajax({
        type: "GET",
        url: "biha4iayz?act=API_EditRecord&rid=" + savedRId + "&_fid_13=" + sundayValue + "&_fid_57=" + mondayValue + "&_fid_58=" + tuesdayValue + "&_fid_59=" + wednesdayValue + "&_fid_60=" + thursdayValue + "&_fid_61=" + fridayValue + "&_fid_62=" + saturdayValue,
        dataType: "xml",
        success: function () {

        },
        fail: loadFail
    });



}

}

最佳答案

您使用 when 的问题是您的两个函数都没有返回任何内容。您的电话基本上相当于:

$.when(undefined, undefined)

您的 saveTimelines 函数是更复杂的函数,因为您在第一个函数的回调中进行了第二个 ajax 调用。更糟糕的是,这些 ajax 调用处于循环状态。 因此,直到循环每次迭代的内部 ajax 调用完成后,您的函数才“完成”。

我强烈建议尝试完全重新设计它以简化事情。如果您可以消除循环以及嵌套的 ajax 调用,这会容易得多。

话虽如此,让我们看看如何克服这个问题。首先要处理内部ajax调用的问题,您可以通过创建自己的延迟对象来解决这个问题。像这样的东西(暂时忽略循环):

function saveOneTimeline(/* any params here, such as i */) {

    // create a deferred object which will be returned by this function and resolved once all calls are complete
    var def = $.Deferred();

    /* ... */

    $.ajax({
        /* ... */    
        success: function (xml) {
            /* ... */                
            $.ajax({
                /* ... */    
                success: function () {
                    // we are done, resolve the deferred object
                    def.resolve();
                }
            });
        }
    });

    // return the deferred object so that the calling code can attach callbacks/use when
    return def;
}

最后,我们之前的方法可以在循环中调用,将返回的延迟对象放入数组中,然后使用 when 返回一个只有在所有延迟对象解析后才会解析的 Promise。它看起来像这样:

function saveTimelines() {
    // an array to store all of the deferreds
    var defs = [];

    for (i=1; i < timelineIndex + 1; i++) {
        defs.push(saveOneTimeline(i));
    }

    // call when on the array of deferred objects and return the resulting promise object
    return $.when.apply($, defs);
}

由于您没有嵌套的 ajax 调用,您的 editSavedTimelines 稍微简单一些。然而,你仍然有一个循环。可以使用非常相似的方法,只不过辅助函数可以直接返回 ajax 调用返回的对象。

如您所见,这一切都非常复杂。尝试消除一些复杂性以避免不必要的努力可能是一个更好的主意。也许如果您可以进行一次批量 ajax 调用而不是循环中的多次调用,那么允许后端代码来处理分离。

关于JavaScript/JQuery : Reload a page AFTER two functions have completed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22052206/

相关文章:

javascript - 亚马逊 s3 : How can I name a new folder after each new username?

javascript - 数据表columnDefs不工作: jquery/javascript

javascript - Rails- Bootstrap 设置

javascript - jQuery div 动画

javascript - 使用基于 keydown() 的 tabindex 将焦点转移到下一个元素

javascript - 添加/删除/替换 html 元素的类

JavaScript 与术语混淆?

jquery - 带有 RSS 新闻提要的事件 JQuery 资源/目录

javascript - 如何用 php 数组值替换 jquery 变量值

javascript - 通过 javascript 将用户可输入的文本存储到隐藏表单中是否危险?