如何在使用setInterval調用函數時傳遞參數。 即setInterval('funca(10,3)',500);
不正確。在setInterval函數中傳遞參數
262
A
回答
436
您需要創建一個匿名函數,以便實際功能不會馬上執行。
setInterval(function() { funca(10,3); }, 500);
34
16
您可以使用匿名函數;
setInterval(function() { funca(10,3); },500);
0
這工作setInterval("foo(bar)",int,lang);
.... 喬恩克萊澤導致我的答案。
29
可以傳遞參數(S)作爲函數對象的屬性,而不是作爲一個參數:
var f = this.someFunction; //use 'this' if called from class
f.parameter1 = obj;
f.parameter2 = this;
f.parameter3 = whatever;
setInterval(f, 1000);
然後在你的功能someFunction
,你將有機會獲得的參數。這在範圍自動進入全局空間的類中特別有用,並且您將失去對稱爲setInterval的類的引用。通過這種方法,上述示例中的「someFunction」中的「parameter2」將具有正確的範圍。
5
報價參數應該足夠:
OK --> reloadIntervalID = window.setInterval("reloadSeries('"+param2Pass+"')" , 5000)
KO --> reloadIntervalID = window.setInterval("reloadSeries("+param2Pass+")" , 5000)
注意每個參數的單引號'
。
與IE8,鉻和Firefox
56
12
setInterval(function(a,b,c){
console.log(a + b +c);
}, 500, [1],[2],[3]);
//note the console will not print 6
// rather it will print 123 in every 500 ms
// because arguments are passed as array not as numbers
// tested in node.js ie 11 and chrome
0
另一解決方案包括在通過您的功能類似的(如果你有動態變量): setInterval('funca('+ x +','+ y +')',500);
-1
您可以使用名爲underscore js的庫。它在綁定方法上提供了一個很好的包裝,並且也是一種更簡潔的語法。讓你在指定的範圍內執行該函數。
_.bind(功能,適用範圍,*參數)匿名函數後
10
setInterval(function,milliseconds,param1,param2,...)
-2
添加parenthesizes似乎這樣的伎倆:
function clock() {
n = 10;
while(--n) {
setInterval(function() {
console.log(n);
}(n), 1000);
}
}
0
我知道這個話題這麼老,但這裏是我的解決方案,在setInterval
函數中傳遞參數。
HTML:
var fiveMinutes = 60 * 2;
var display = document.querySelector('#timer');
startTimer(fiveMinutes, display);
的JavaScript:
function startTimer(duration, display) {
var timer = duration,
minutes, seconds;
setInterval(function() {
minutes = parseInt(timer/60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
--timer; // put boolean value for minus values.
}, 1000);
}
9
到目前爲止,最實用的答案是一個由tvanfosson給出的,所有我能做的就是給你一個更新的版本ES6:
setInterval(()=>{ funca(10,3); }, 500);
相關問題
- 1. 在函數中使用setInterval並在其中傳遞參數
- 2. .then()不能在setInterval函數中傳遞參數?
- 3. javascript - 將對象參數傳遞給setInterval
- 4. 在C++中將函數參數作爲函數參數傳遞
- 5. 在Matlab中傳遞函數參數
- 6. 在flex函數中傳遞參數
- 7. 如何在函數中傳遞參數?
- 8. 在函數pdo中傳遞參數php
- 9. 參數傳遞給函數
- 10. 參數傳遞給函數
- 11. VIM函數參數傳遞
- 12. 蟒傳遞函數參數
- 13. 傳遞函數的參數
- 14. jQuery函數傳遞參數
- 15. Python:函數,傳遞參數
- 16. 傳遞函數參數在光澤ř
- 17. 傳遞函數參數值NULL在C
- 18. 傳遞的參數函數read.table中的()
- 19. 當函數作爲參數傳遞時,參數如何傳遞給該函數?
- 20. 傳遞函數的參數作爲參數傳遞給另一個函數
- 21. 在函數中將類/實例函數作爲參數傳遞
- 22. 函數參數傳遞和函數重定義在Julia中
- 23. 將參數傳遞給函數,該參數必須作爲參數傳遞給python中的另一個函數
- 24. 傳遞給函數的參數數量?
- 25. 傳遞函數數組作爲參數
- 26. 將數組傳遞給函數參數
- 27. 傳遞參數成函數的函數調用的參數
- 28. 傳遞一個在多個參數的函數參數在JavaScript
- 29. 在postgresql更新函數中傳遞參數和參考
- 30. 如何在函數中傳遞函數?
什麼應該是動態參數? – rony36 2013-08-22 17:16:45
@ rony36 - 你可能想要一個爲你創建間隔計時器的函數。將參數傳遞給函數,使其值在函數閉包中捕獲,並在定時器到期時保留。 `function createInterval(f,dynamicParameter,interval){setInterval(function(){f(dynamicParameter);},interval); }`然後稱之爲`createInterval(funca,dynamicValue,500);`顯然你可以擴展這個參數。請使用更多的描述性變量名稱。 :) – tvanfosson 2013-08-22 18:12:44
@tvanfosson - 如何清除這種間隔? – Yong 2016-10-03 06:33:09