请建议如何将参数传递到使用setInterval的函数中。

我的例子setInterval(funca(10,3), 500);是不正确的。


当前回答

此外,与IE支持> 9,你可以传递更多的变量内部设置间隔,将采取你的函数。例句:

function myFunc(arg1, arg2){};
setInterval(myFunc, 500, arg1, arg2);

问候!

其他回答

我知道这个话题是如此古老,但这是我的解决方案传递参数在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);
}

现在用ES5,绑定方法函数原型:

setInterval(funca.bind(null,10,3),500);

参考这里

将它们作为参数添加到setInterval:

setInterval(funca, 500, 10, 3);

你问题中的语法使用了eval,不建议练习。

你可以使用匿名函数;

setInterval(function() { funca(10,3); },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);