2010-11-15 167 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错误以确保图像在请求失败时也隐藏。