0

好吧,我一直在处理的代码出现了新问题;单击打开项目时,我无法停止自动刷新,然后单击关闭项目后重新启动。

<script type='text/javascript'>
var auto_refresh = setInterval( function() { $('#body').load('body.php').fadeIn('slow'); }, 1000);
$(document).ready(function(){
            $('.star, .ship').click(function(){
                    var name = $(this).attr('name');
                    var type = $(this).attr('type');
                    var linkvar = 'content.php?lid=';
                    var link_full = linkvar + name;
                    $('#box').removeClass('hidden');
                    $('#box_content').addClass(type);
                    if(auto_refresh)
                    {
                        clearInterval(auto_refresh);    
                    }
                    $('#box_content').load(link_full);
                });
$('#close_this_box').click(function(){
    $('#box').addClass('hidden'); $('#box_content').empty();
    var auto_refresh = setInterval( function() { $('#body').load('body.php').fadeIn('slow'); }, 1000);
    });
$('.ship, .star').hover(
    function()
    { 
        $(this).children('div').removeClass('hidden');
    }, 
    function(){ $(this).children('div:nth-child(2)').addClass('hidden');});
});
</script>

它要么继续自动刷新,要么根本不加载#body div。

4

1 回答 1

3

在函数中删除var关键字 beforeauto_refresh$('#close_this_box').click(

$('#close_this_box').click(function(){
    $('#box').addClass('hidden'); $('#box_content').empty();
    auto_refresh = setInterval( function() { $('#body').load('body.php').fadeIn('slow'); }, 1000);
});

使用var那里会生成一个名为的局部变量auto_refresh,而不是修改全局变量auto_refresh

于 2013-09-22T01:54:19.417 回答