2010-11-15 172 views
1

我目前已經實現了ajax來刷新我的內容,但我想添加一個延遲 - 任何人都知道我可以如何將它添加到我有的代碼中?ajax加載圖像延遲?

$.ajax({ 
    url: "<?php echo site_url('apply/processpersonaldetails'); ?>", 
    type: 'POST', 
    data: form_data, 
    success: function(msg) { 
     $('#content').empty().html('<img src="../images/ajaxloader.gif" />'); 
     $('#content').html(msg); 
    } 
}); 

不好意思啊,我的意思是之前新的內容加載,我想顯示加載圖像顯示在新的內容之前的延遲......這是否有意義?

+0

你能更具體一點嗎?你什麼時候需要延遲?在ajax調用之間?在用新的內容替換舊內容之前?你的問題並不清楚。 – 2010-11-15 14:42:46

+0

我有完全相同的問題,加載圖標只在運行完ajax完成後「運行」。 – Dryadwoods 2013-01-28 09:43:56

回答

0

搜索Google 的setInterval和setTimeout的會做你想做的結合使用jQuery

1

我不太明白你在你的情況下延遲的意思,但你可以使用setTimeout功能安排的執行一些回調在稍後的時刻:

window.setTimeout(function() { 
    alert('this will be executed 3 seconds later'); 
}, 3000); 
0

我不知道我完全理解你的問題,但假設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錯誤以確保圖像在請求失敗時也隱藏。