使用jQuery延迟重新加载页面

时间:2010-02-18 10:11:56

标签: jquery delay

我需要延迟重新加载页面。现在,我有这个:

$('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();?>';
                });
            });

这显然不起作用。任何人都知道如何延迟重载至少3秒左右?

4 个答案:

答案 0 :(得分:15)

使用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);               
                });
            });

答案 1 :(得分:3)

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();?>'; }

答案 2 :(得分:1)

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);
                    });
                });

答案 3 :(得分:0)

尝试这个,保存混合php和jQuery,"."重新加载当前页面。

setTimeout(window.location = ".", 600);