Javascript Jquery ajax在获取页面之前等待

Javascript Jquery ajax在获取页面之前等待,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,Hellp people…让我们看一段简单的ajax代码 $.ajax({ method: "POST", url: "some.php", data: { name: "John", location: "Boston" } }) .done(function( msg ) { alert(msg.html()); }); 等待5秒钟后,我正在尝试获取msg.html()?所以过程如下 将数据发送到s

Hellp people…让我们看一段简单的ajax代码

    $.ajax({
      method: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert(msg.html());
      });
等待5秒钟后,我正在尝试获取
msg.html()
?所以过程如下

  • 将数据发送到
    some.php
  • 等待5秒钟
  • 然后返回html页面数据

  • 我们如何才能做到这一点

    使用
    setTimeout
    ,setTimeout()方法在指定的毫秒数后调用函数或计算表达式。该函数只执行一次

    如果您想获取
    msg
    html
    ,那么首先确保
    msg
    是有效的html字符串,在获取
    .html()
    之前,请通过
    $(msg)
    将其转换为jQuery对象


    你可以把它传递到你想要的地方。

    你正在寻找
    setTimeout
    @Slaks
    setTimeout
    我们必须在
    内部给出完成的功能还是
    .success
    功能?你想开始等待时就调用
    setTimeout
    。等待的原因是。。。my
    some.php
    将在5秒后加载数据(数据库获取)。。。。只有在加载之后,我才能通过ajax获取整个页面的html。。设置超时是否足以执行此操作?有两件事,1)在5秒钟后加载some.php,然后在加载完成时立即发出警报。2.)加载some.php,然后在完全加载后5秒显示警报。你到底想要什么?我想做2。)加载一些.php,然后在5秒钟后,当它完全加载时显示警报。好的,这就是我在回答中所做的,对于这个
    setTimeout
    将被筛选,请参阅我更新的回答。@user3436481为什么不让
    some.php
    返回数据,而不是第二次调用来加载它?
    $.ajax({
        method: "POST",
        url: "some.php",
        data: {
          name: "John",
          location: "Boston"
        }
      })
      .done(function(msg) {
    
        setTimeout(function() {
          alert($(msg).html());
        }, 5 * 1000);
    
      });
    
    $.ajax({
          type : 'POST',
          url  : 'some.php',
          data :{ name : 'John', location : 'Boston'},
          beforeSend : function(){
            //you can do whatever you want here (loading gif etc.) until some.php responds 
          },
          complete : fuction(){
        //Here is for after responding
          },
          success : function(data){
          //data is the response of some.php, you can use the data to fill some elements in DOM etc.
        }
    
    
        });
    
        setTimeout(function(){
        //the codes you write here fires after 5 secs
        },5000);