2013-03-08 55 views
0

爲什麼setInterval(fetchData(), 1000*60);在Chrome中運行時出現錯誤Firefox;我想將一個變量傳遞給fetchdata。解決Firefox在setInterval上拋出錯誤。

function fetchData(var) { 
     ..... 
    }; 

    fetchData(var); //run once 
    setInterval(fetchData(var), 1000*60); //then repeat 

堆棧跟蹤:

[10:26:49.764] Error: useless setInterval call (missing quotes around argument?) 
[email protected]://localhost:8000/mainx.js:169 
[email protected]://localhost:8000/lib/angular.js:2809 
[email protected]://localhost:8000/lib/angular.js:2819 
@http://localhost:8000/lib/angular.js:4639 
applyDirectivesToNode/nodeLinkFn/<@http://localhost:8000/lib/angular.js:4218 
[email protected]://localhost:8000/lib/angular.js:117 
[email protected]://localhost:8000/lib/angular.js:4203 
[email protected]://localhost:8000/lib/angular.js:3851 
[email protected]://localhost:8000/lib/angular.js:3763 
bootstrap/</<@http://localhost:8000/lib/angular.js:932 
[email protected]://localhost:8000/lib/angular.js:7840 
[email protected]://localhost:8000/lib/angular.js:7920 
bootstrap/<@http://localhost:8000/lib/angular.js:930 
[email protected]://localhost:8000/lib/angular.js:2802 
[email protected]://localhost:8000/lib/angular.js:929 
[email protected]://localhost:8000/lib/angular.js:904 
@http://localhost:8000/lib/angular.js:14527 
p.Callbacks/[email protected]://localhost:8000/lib/jquery-1.8.2.min.js:2 
p.Callbacks/[email protected]://localhost:8000/lib/jquery-1.8.2.min.js:2 
[email protected]://localhost:8000/lib/jquery-1.8.2.min.js:2 
[email protected]://localhost:8000/lib/jquery-1.8.2.min.js:2 

回答

2

此語法不正確。你不能像這樣使用setTimeout或setInterval傳遞變量。

setInterval(fetchData(var), 1000*60); //then repeat 

改爲使用以下內容。

setInterval(function() { 
    fetchData(var); 
}, 1000*60); 
3

使用

setInterval(function() {fetchData(var)}, 1000*60); //then repeat 

你在做實際上並沒有經過你的說法的。您需要將其包裝在anonymous function中。

相關問題