ajax,如果3秒后刷新成功



title说明了一切,脚本当前的工作方式是,如果成功,它会从我的php代码中返回一条消息,我希望它也能在3秒后刷新页面

$(function(){
        $('button[type=submit]').click(function(e){
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "postadvert.php",
                data: $("#myform").serialize(),
                beforeSend: function(){
                    $('#result').html('<div class="success"><img src="../../images/loading-blue.gif" width="25" /></div>');
                },
                success: function(data){
                    $('#result').html(data),
                    $('#result2').html('<meta http-equiv="refresh" content="3">'); // i added that doesn't working
                }
            });
        });
    });

有几件事。首先,在成功函数的第一行后面需要一个分号。接下来,您可以在javascript中使用setTimeout函数,在这里您可以传递一个函数和一个以毫秒为单位的等待时间。最后,您可以调用location.reload()来刷新页面。

success: function(data){
   $('#result').html(data);
   setTimeout(function(){location.reload();},3000);
}

在成功回调中包括以下内容:

window.setTimeout(function() { 
  document.location.href = document.location.href; 
}, 3000);

自动分配给document.location.href会使浏览器加载如此分配的URL;因此,将其自身的值重新分配给它会导致刷新。window.setTimeout()调用告诉浏览器等待3秒钟,然后运行作为其第一个参数给定的函数。

最新更新