2015-02-10 41 views
1

所以我在函數之外聲明空白變量。setTimeout導致Uncaught TypeError:數字不是函數

//To be Timeouts 
var progressionTimer; 
var nextTimer; 
var cycleTimer; 

,然後內但是這些聲明的一個被稱爲

nextTimer(); 

我在Chrome控制檯每次函數

progressionTimer = setTimout(loadNextFunction, 2000); 
progressionTimer(); 

nextTimer = setTimeout(loadOutsideFunction, 2000); 
nextTimer(); 

//etc 

/火狐/等本

Uncaught TypeError: number is not a function 
填充

它的功能ns完全按照預期和clearTimeout工作沒有問題,但控制檯錯誤只是令我沮喪,任何人都可以解決這個問題,而不失去功能,仍然有clearTimeout工作?

+0

的返回值,它是一個id爲您將傳遞給clearTimout以清除它的超時。 – 2015-02-10 23:45:22

回答

1

setTimeout返回一個處理程序,一個ID可讓您引用超時,因此您可以用clearTimeout(數字)來清除它。

返回可以執行的功能,這就是問題所在,你想因爲nextTimer不是一個函數來執行的setTimeout

nextTimer = setTimeout(loadOutsideFunction, 2000); 
nextTimer(); // not a function, but a number referencing the timeout ? 

clearTimeout(nextTimer); // works just fine 
+0

謝謝!我不得不稱之爲setTimeout。所以現在我剛剛刪除了 nextTimer(); 及其所有功能沒有問題。 – 2015-02-10 23:56:39

+0

不客氣! – adeneo 2015-02-11 00:01:08

相關問題