使用ajax success和setTimeout

时间:2016-01-29 21:03:47

标签: jquery ajax twitter-bootstrap bootstrap-modal

我尝试在jQuery ajax请求的成功函数页面刷新后显示引导模式。我认为发生的事情是,一旦页面刷新,setTimeout就会被擦掉,所以它永远不会被触发。我需要页面来刷新内容,但也希望为用户提供一个显示发生了什么的对话框。

这是我的代码:

$.ajax({
            type: "POST",
            url: '@Url.Action("UpdateVideoUrl", "ManageVideos")',
            data: JSON.stringify(model),
            contentType: 'application/json',
            success: function () {
                location.reload(true);
                $(window).scrollTop(0);
                setTimeout(function () {
                    $("#SuccessModal").modal('show');
                }, 200);

            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus + ": " + errorThrown + "." + "  Please see above if applicable");
            }
        });

如果我将超时设置为100,模式开始显示,页面刷新,我丢失了模态。有没有办法完成这项任务?

1 个答案:

答案 0 :(得分:0)

我没有刷新页面然后显示模态,而是意识到我需要显示模态,当模态关闭时,触发location.reload(true).animate命令:

//Put page to on refresh
$(document).ready(function () {
    $("html,body").animate({ scrollTop: 0 }, 100); //100ms for example
});

//Refresh page after modal dialog is closed
function ReloadPage() {
    location.reload(true);
}