2011-07-02 81 views
0

我使用下面的腳本來刷新攝像頭圖像。它每18秒刷新一次,刷新8次後停止刷新。我有一種感覺,它不能正確取消它。因爲過了一段時間,我看到加載標誌出現故障。我的腳本有問題嗎?用jquery asynch刷新圖像

var count = 0; 
function CamRefresh() { 
    count++; 
    if(count > 8) clearInterval(timeout); 
    updatecam(); 
} 
var timeout = setInterval(CamRefresh, 18000); 

}); 

function updatecam() { 
$('#loading').show('fast'); 
$('#activecam').attr('src', 'http://weeg.binaer.no/weeg_com/nesjordet.jpg?time='+Date()); 
$('#loading').hide('slow'); 
$('#currentswitch').hide(1500); 
$('#dayswitch').show(); 
} 
+0

那是什麼額外的'});'for? – Dogbert

回答

1

我懷疑你loading元素在你的updatecam()功能的動畫有一個時機的問題,如果一個動畫還不完 - 我會在那裏鏈接一個stop()。試試這個:

function updatecam() 
{ 
    $('#loading').show('fast'); 
    $('#activecam').attr('src', 'http://weeg.binaer.no/weeg_com/nesjordet.jpg?time='+Date()); 
    $('#loading').stop().hide('slow'); 
    $('#currentswitch').hide(1500); 
    $('#dayswitch').show(); 
} 

另外請注意,只是設置src屬性不會等到圖像實際加載。爲此,您可以使用加載函數掛鉤:

$('#activecam').attr('src', 'http://weeg.binaer.no/weeg_com/nesjordet.jpg?time='+Date()) 
       .load(function() 
       { 
        $('#loading').stop().hide('slow'); 
        $('#currentswitch').hide(1500); 
        $('#dayswitch').show();  
       }); 
1
var timeout = setInterval("CamRefresh()", 18000); 

我也懷疑是多餘的});可能會使你count VAR而不是在全球範圍內,這是在setInterval將尋找它。要麼確保count是在全球範圍內也可以做這樣的事情:

function CamRefresh() { 
    if (!CamRefresh.count) CamRefresh.count = 0; 
    CamRefresh.count++; 
    if(CamRefresh.count > 8) clearInterval(timeout); 
    updatecam(); 
} 
+0

新的Camrefresh不起作用。 – mark