请建议如何将参数传递到使用setInterval的函数中。
我的例子setInterval(funca(10,3), 500);是不正确的。
请建议如何将参数传递到使用setInterval的函数中。
我的例子setInterval(funca(10,3), 500);是不正确的。
当前回答
setInterval(function(a,b,c){
console.log(a + b +c);
}, 500, 1,2,3);
//note the console will print 6
//here we are passing 1,2,3 for a,b,c arguments
// tested in node v 8.11 and chrome 69
其他回答
您需要创建一个匿名函数,这样实际的函数就不会立即执行。
setInterval( function() { funca(10,3); }, 500 );
将它们作为参数添加到setInterval:
setInterval(funca, 500, 10, 3);
你问题中的语法使用了eval,不建议练习。
另一个解决方案是像这样传递你的函数(如果你有动态变量): setInterval(“funca (' + x + ', ' + y + ') ', 500);
这对我很有效
let theNumber = document.getElementById('number');
let counter = 0;
function skills (counterInput, timer, element) {
setInterval(() => {
if(counterInput > counter) {
counter += 1;
element.textContent = `${counter} %`
}else {
clearInterval();
}
}, timer)
}
skills(70, 200, theNumber);
const designated = "1 jan 2021"
function countdown(designated_time){
const currentTime = new Date();
const future_time = new Date(designated_time);
console.log(future_time - currentTime);
}
countdown(designated);
setInterval(countdown, 1000, designated);
有很多方法可以做到这一点,我个人认为这是干净和甜蜜的。