显示消息后重新加载页面,jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8404970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:29:33  来源:igfitidea点击:

Reload page after message is shown, jQuery

jqueryajaxreload

提问by Ilja

Right now I have a form where user enters the info. It is than processed bu jQuery ajax function and is set to return false;so no page reload happens after user submits a form. Although I need to reload page, but if I remove return false;it refreshes page before success message is shown (this message is shown after user submits data).

现在我有一个用户输入信息的表单。它比处理过 jQuery ajax 函数并设置为return false;因此在用户提交表单后不会发生页面重新加载。虽然我需要重新加载页面,但如果我删除return false;它会在显示成功消息之前刷新页面(此消息在用户提交数据后显示)。

     $.ajax({
      type: "POST",
      url: "scripts/process.php",
      data: dataString,
      success: function() {
       $("#st_message").html("<p> Your article was successfully added!</p>");
       //I need to reload page after the above message is shown
      }
     });
    return false;

So how can I reload page after the <p> Your article was successfully added!</p>message is shown, with a slight delay say 2 - 3 seconds, so user can actually read the message.

那么如何在显示<p> Your article was successfully added!</p>消息后重新加载页面,稍微延迟 2 - 3 秒,以便用户可以实际阅读消息。

回答by Rion Williams

You could add a delay by using the setTimeout()function, such as:

您可以使用该setTimeout()函数添加延迟,例如:

// This will reload the page after a delay of 3 seconds
window.setTimeout(function(){location.reload()},3000)

For your needs:

满足您的需求:

$.ajax({
      type: "POST",
      url: "scripts/process.php",
      data: dataString,
      success: function() {
           $("#st_message").html("<p> Your article was successfully added!</p>");
           window.setTimeout(function(){location.reload()},3000)
      }
});
return false;

Working Example

工作示例

回答by Jakub

Do it with something like:

用类似的方法做:

function delayedRedirect(){
    window.location = "/index.php"
}

setTimeout('delayedRedirect()', 3000)

setTimeoutis used to delay the redirect function call, in this case you can redirect somewhere to a new URL (just in case you need that too).

setTimeout用于延迟重定向函数调用,在这种情况下,您可以将某处重定向到新的 URL(以防万一您也需要)。

Otherwise use location.reload()to reload page.

否则用于location.reload()重新加载页面。

回答by Guffa

Use the setTimeoutmethod to get the delay, and the reloadmethod to reload the page. Note the trueparameter in the reloadcall to ensure that the page is actually reloaded, and not just repainted from the cache.

使用setTimeout获取延迟的reload方法,以及重新加载页面的方法。请注意调用中的true参数,reload以确保实际重新加载页面,而不仅仅是从缓存中重新绘制。

window.setTimeout(
  function(){
    location.reload(true)
  },
  3000
);

回答by David Laberge

Do you mean something like : window.location.reload()in JavaScript

你的意思是这样的:window.location.reload()在 JavaScript 中

回答by mithunsatheesh

give it using

给它使用

setTimeout("window.location='yourpage.php'",3000);

in your code :

在您的代码中:

 $.ajax({
       type: "POST",
       url: "scripts/process.php",
       data: dataString,
       success: function() {
        $("#st_message").html("<p> Your article was successfully added!</p>");
        setTimeout("window.location='yourpage.php'",3000);//reload after 3 sec.
       }
      });
 return false;

回答by chuckfinley

Using timeout is a really bad idea.

使用超时是一个非常糟糕的主意。

回答by jlrvpuma

 $.ajax({
  type: "POST",
  url: "scripts/process.php",
  data: dataString,
  success: function() {
  var miliseconds = 2000;

   $("#st_message").html("<p> Your article was successfully added!</p>");
   setTimeout("window.location=window.location", miliseconds);
   //I need to reload page after the above message is shown
  }
 });

回答by Maher

<script type="text/javascript">
 //
 //your action here
 //
 window.setTimeout(function(){self.parent.location="?list"},3000);//after 3 sec go to (?list or example.html page)
 return false;
</script>

回答by staranium

exemple :

例子:

success: function(data){
            $("#loading-img").css("display","none");
                 swal({
                  title: "Félécitations",
                  text: "Votre demande a été transmise au service concerné",
                  icon: "success",
                  button: "OK",
                });
            window.setTimeout(function(){location.reload(true)},3000)
            }
            
            });
            return false;
            }