2013-09-22 198 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

你是什麼意思? –