使用 jQuery 延迟页面重新加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2287742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delaying the reload of a page using jQuery
提问by Teej
I need to delaying the reloading of the page. Right now, I have this:
我需要延迟页面的重新加载。现在,我有这个:
$('div.navigation_sorter ul li a.delete').click(function(event){
event.preventDefault();
var li = $(this).closest('li');
$.post('<?php echo site_url('admin/navigation/delete_link');?>', { link: li.attr('id')}, function(data){
$('<div class="feedback info"><p>'+ data +' has been deleted</p></div>').insertAfter('.navigation_sorter').delay(800);
window.location = '<?php echo current_url();?>';
});
});
This obviously doesn't work. Anyone have an idea how to delay the reloading for atleast 3 seconds or so?
这显然行不通。任何人都知道如何将重新加载至少延迟 3 秒左右?
回答by Ikke
Use setTimeoutto execute a function after a certain period:
使用setTimeout在一段时间后执行函数:
$('div.navigation_sorter ul li a.delete').click(function(event){
event.preventDefault();
var li = $(this).closest('li');
$.post('<?php echo site_url('admin/navigation/delete_link');?>', { link: li.attr('id')}, function(data){
$('<div class="feedback info"><p>'+ data +' has been deleted</p></div>').insertAfter('.navigation_sorter')
setTimeout(function(){
window.location = '<?php echo current_url();?>';
}, 3000);
});
});
回答by Ed James
The jQuery .delay method is only for delaying function chains, you want to use SetTimeout():
jQuery .delay 方法仅用于延迟函数链,您要使用 SetTimeout():
$('div.navigation_sorter ul li a.delete').click(function(event){ event.preventDefault(); var li = $(this).closest('li'); $.post('<?php echo site_url('admin/navigation/delete_link');?>', { link: li.attr('id')}, function(data){ $('<div class="feedback info"><p>'+ data +' has been deleted</p></div>').insertAfter('.navigation_sorter'); setTimeout('reload()', 800); }); });
function reload() { window.location = '<?php echo current_url();?>'; }
回答by Artem Barger
setTimeout
should do the job,
setTimeout
应该做这项工作,
$('div.navigation_sorter ul li a.delete').click(function(event)
{
event.preventDefault();
var li = $(this).closest('li');
$.post('<?php echo site_url('admin/navigation/delete_link');?>',
{
link: li.attr('id')
}, function(data)
{
$('<div class="feedback info"><p>'+ data +' has been deleted</p>' +
'</div>').insertAfter('.navigation_sorter').delay(800);
setTimeout( function()
{
window.location = '<?php echo current_url();?>';
}, 3000);
});
});
回答by Frithir.com
Try this one, saves mixing php and jQuery, the "."
reloads the current page.
试试这个,保存混合 php 和 jQuery,"."
重新加载当前页面。
setTimeout(window.location = ".", 600);