请建议如何将参数传递到使用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函数。

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);
}

引用论点就足够了:

OK --> reloadIntervalID = window.setInterval( "reloadSeries('"+param2Pass+"')" , 5000)

KO --> reloadIntervalID = window.setInterval( "reloadSeries( "+param2Pass+" )" , 5000)

注意每个参数的单引号。

通过IE8, Chrome和FireFox测试

我在Vue应用程序中遇到了同样的问题。在我的情况下,这种解决方案仅适用于匿名函数声明为箭头函数,关于在mounted()生命周期钩子上的声明。

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

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

参考这里

您需要创建一个匿名函数,这样实际的函数就不会立即执行。

setInterval( function() { funca(10,3); }, 500 );