2011-10-11 64 views
0

在通過表單操作方法將HTML表單發佈到遠程服務器之前,我使用jQuery將表單發佈到數據庫。在腳本返回true之前暫停jQuery函數5秒併發布

所有的工作正常,但我想建立一個消息之前的服務器POST,所以有沒有一種方式,就在我的jQuery函數返回true,讓它暫停5秒,並顯示一條消息?

... 
    $.ajax({ 
     type: "POST", 
     url: "myform.php", 
     data: dataString, 
     success: function() { 
      $('#details').html("<div id='post-message'></div>"); 
      $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch</h2>") 
      //.append("<p>Thank you</p>") 
      .hide() 
      .fadeIn(1500, function() { 
       // $('#message').append("<img id='tick' src='images/tick.png' />"); 
      }); 
     } 
    }); 
    return true; 
}); 
... 

我理想的情況是在腳本返回true並完成之前需要5秒的暫停 - 非常感謝任何幫助!目前該消息沒有時間顯示,因爲該腳本返回true和帖子!

感謝

+0

是否有使用setTimeout的方法有任何問題。 – Krasimir

+1

這裏沒有任何意義,它會異步運行,並始終返回true。 –

回答

0

你可以在成功的功能使用setTimeout('func', 5000);

function doItLater() 
{ 
    $('#details').html("<div id='post-message'></div>"); 
    $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch</h2>") 
    //.append("<p>Thank you</p>") 
    .hide() 
    .fadeIn(1500, function() { 
     // $('#message').append("<img id='tick' src='images/tick.png' />"); 
    }); 
} 
$.ajax({ 
     type: "POST", 
     url: "myform.php", 
     data: dataString, 
     success: function() { 
     // do stuff now ... 
     // wait 5 seconds 
     window.setTimeout('doItLater', 5000); 
     return true; 
    }); 
}); 
+0

他說,前,不是發佈。 –

+0

是的,我在談論pre。在上面的例子中,我更加清楚了。 – PiTheNumber