2012-11-05 33 views
0

這就是我想要的:當我懸停在div上時超時,取消如果我再次懸停在div上。使用setTimeout/Cleartimeout時出現非法令牌ILLEGAL

該代碼的作品,如果我刪除了取消功能。因此,顯示和隱藏分區工作正常。

這是我的代碼:

$(document).ready(function() 
    { 
     var timeoutID; 
     function clearAlert() 
     { 
      window.clearTimeout(timeoutID); 
     }​ 

     if ($('.txtVote').text() == "Avgi stemme") 
     { 
      $('#starInfo').hover(
      function() 
      { 
       clearAlert(); 
       $('#CarDetailsButtons').css('right', '10px'); 
       $('#voteStars').show(); 
      }, 
      function() 
      { 
       console.log("hide iniated"); 
       timeoutID = window.setTimeout(hideStars, 2000); 
       console.log("hide executed"); 
      });  
     } 

     function hideStars() 
     { 
      console.log("hideStars ran"); 
      $('#CarDetailsButtons').css('right', '45px'); 
      $('#voteStars').hide(); 
     } 
}); 

而且,這是我的錯誤:

Uncaught SyntaxError: Unexpected token ILLEGAL

這之後的行上window.clearTimeout(timeoutID);

任何人都能夠看到我是什麼做錯了? :)

編輯: * WORKS *

所以,看來我有某種複製粘貼的bug。我手動編寫了clearAlert函數,就像以前一樣,現在它可以工作。

感謝您的所有意見!

+2

'var timeouteId;'versus'timeoutID' - 更改其中一個 – mplungjan

+0

沒有工作:(但我更新了代碼,仍然是同樣的錯誤,我懷疑我甚至需要var timeoutID; – sindrem

+1

將var移動到全局範圍:'window.timeoutID =「」;' – mplungjan

回答

0

我建議你移動文檔準備這樣的地區以外的功能:

function hideStars() 
    { 
     console.log("hideStars ran"); 
     $('#CarDetailsButtons').css('right', '45px'); 
     $('#voteStars').hide(); 
    } 

function clearAlert() 
    { 
     window.clearTimeout(timeoutID); 
    }​ 

$(document).ready(function() 
    { 
    var timeoutID; 


    if ($('.txtVote').text() == "Avgi stemme") 
    { 
     $('#starInfo').hover(
     function() 
     { 
      clearAlert(); 
      $('#CarDetailsButtons').css('right', '10px'); 
      $('#voteStars').show(); 
     }, 
     function() 
     { 
      console.log("hide iniated"); 
      timeoutID = window.setTimeout(hideStars, 2000); 
      console.log("hide executed"); 
     });  
    } 


}); 
+0

請參閱我的解決方案的編輯。無論如何謝謝:) – sindrem

+0

偉大的編輯.......順便說一句。 – Jai

2

試試這個:

var that = this; 
that.timeoutID = null; 

... 

that.timeoutID = ... 

OR

必須聲明VAR timeoutID = NULL;全球。所以你在gloabl上下文中有var,而不在$(document).ready的函數上下文中。

最好的解決方案是寫一個帶有定時器的js-class。所以你沒有全局性的衝突。

問候!

相關問題