2013-09-22 210 views
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。

+0

'#body'的'.ship','.star'或'#close_this_box'子元素? –

+0

他们都是#body的孩子。如果你想我也可以发布HTML(帕特里克埃文的答案'固定'的问题,虽然现在我有刷新和东西的问题,所以我会愿意发布更多大声笑) – Gmz1023

回答

3

auto_refresh之前删除var关键字使用var$('#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); 
}); 

存在的,名为auto_refresh而不是修改全局变量auto_refresh一个局部变量。

+0

做到了这一点。我现在觉得自己像个白痴。谢谢:D 编辑:Actualy scratch,它看起来像是在工作,但显然是停滞在网站上。 – Gmz1023

+0

您也可以将'auto_refresh'变量的声明移动到'.ready'函数中,该函数将保持它在您定义的事件函数中可见,并保持全局空间清洁。但是它对于你在'.ready'函数之外声明的函数是不可见的 –

+0

你是什么意思? –