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

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


当前回答

你可以使用一个名为下划线js的库。它对bind方法进行了很好的包装,语法也更简洁。允许您在指定范围内执行函数。

http://underscorejs.org/#bind

_。绑定(函数,作用域,*参数)

其他回答

     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("foo(bar)",int,lang);....Jon Kleiser带我找到了答案。

引用论点就足够了:

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

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

注意每个参数的单引号。

通过IE8, Chrome和FireFox测试

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

有很多方法可以做到这一点,我个人认为这是干净和甜蜜的。