2012-11-28 24 views
0

我很新的JavaScript,所以請原諒我,如果這是一個愚蠢的問題。我使用下面的代碼讓圖像滑動到屏幕上,並使用另外2個圖像一個接一個地顯示。這適用於Chrome和IE 7-9。不幸的是,在Firefox上,我收到一條錯誤消息:SetInterval在Firefox中看不到功能

move is not defined [mover = setInterval(move,1000); ]

我的代碼:

//define variables 
var mover = 0 
var bubble1move = 0 
var bubble2move = 0 


if(mover != 0) 
{//interval is finished 
    function move() 
    { 
     console.log("moving") 
     clearInterval(mover) 
     var moving_img = document.getElementById("i_sliding_image") 
     var left = 0 
     function frame() 
     {  
      left -= 2 // update parameters 
      moving_img.style.left = left + 'px'// show frame 
      if (left == -274) // check finish condition 
      { 
       clearInterval(id)     
       bubble1move = setInterval(function() {bubble1()}, 2000); 
      } 
     } 
     var id = setInterval(frame, 10) // draw every 10ms 
    }  
} 
if(bubble1move != 0) 
{//interval is finished 
    function bubble1() 
    { 
     clearInterval(bubble1move);  
     document.getElementById("img-bubble1").style.zIndex = "1"; 
     bubble2move = setInterval(function() {bubble2()}, 2000); 
    } 
} 
if(bubble2move != 0) 
{//interval is finished 
    function bubble2() 
    { 
     clearInterval(bubble2move) 
     var vBubble2 = document.getElementById("img-bubble2").style 
     vBubble2.zIndex = "1"; 
    } 
} 
window.onload = function initialiser() 
{ 
    mover = setInterval(move, 1000);//initial time to animation 
} 

所有getElementByIds越來越包含圖像div標籤。

謝謝你的時間。

+1

您的移動定義應該在if語句中嗎?嘗試在其他地方定義函數,並簡單地從if中調用它。 – agryson

+0

你所有的語句終結者在哪裏? ';' –

+0

謝謝agryson,那就是問題 – gaynorvader

回答

1

移動你的功能if之外之前。沒有理由在if之內。由於Firefox解釋功能的方式(與其他瀏覽器不同),它在Firefox上不起作用。

有關更多信息,請參閱this question

感謝@freakish的鏈接。

+0

這是不正確的:它在FireFox上不起作用,因爲FireFox解釋'function' stetements與其他瀏覽器有點不同,(在Chrome中無論你定義函數,它總是可見的) 。閱讀此更多細節:http://stackoverflow.com/questions/4069100/why-cant-i-use-a-javascript-function-before-its-definition-inside-a-try-block它解決了這個問題,所以我不會給-1,但我也不會給+1。 – freakish

+0

@freakish,謝謝 - 添加到答案。解決方案雖然保持不變。 – MrCode

2

看起來像你正在初始化你的移動變量在你的js開始爲0,你的if語句只聲明瞭函數,如果mover != 0。初始化mover = 1;或使用if語句之外的函數(推薦)。你只是試圖調用move()存在