我不知道我完全理解你的問題,但假設ajaxloader.gif是加載圖像,它將永遠不會顯示,因爲它會通過接收到的內容立即更換。
ajax請求本身就是一個足夠的延遲。我覺得從成功處理了移動<img
線就足以看到圖像了一會兒才新內容到達:
// Show the loading image before firing the ajax request
$('#content').html('<img src="../images/ajaxloader.gif" />');
$.ajax({
url: "<?php echo site_url('apply/processpersonaldetails'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
// New content replaces the loading image
$('#content').html(msg);
}
});
如果您仍想展示新內容之前的額外延遲,你可以使用setTimeout的方法像這樣:
// Show the loading image before firing the ajax request
$('#content').html('<img src="../images/ajaxloader.gif" />');
$.ajax({
url: "<?php echo site_url('apply/processpersonaldetails'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
// Insert one second delay before showing new content (not sure why)
window.setTimeout(function() {
// New content replaces the loading image
$('#content').html(msg);
}, 1000);
}
});
考慮處理ajax錯誤以確保圖像在請求失敗時也隱藏。
你能更具體一點嗎?你什麼時候需要延遲?在ajax調用之間?在用新的內容替換舊內容之前?你的問題並不清楚。 – 2010-11-15 14:42:46
我有完全相同的問題,加載圖標只在運行完ajax完成後「運行」。 – Dryadwoods 2013-01-28 09:43:56