Pass parameters in setInterval function

stop setinterval
setinterval in angular
clearinterval
settimeout pass parameters
setinterval react
nodejs setinterval
settimeout vs setinterval
javascript settimeout loop

Please advise how to pass parameters into a function called using setInterval.

My example setInterval(funca(10,3), 500); is incorrect.

You need to create an anonymous function so the actual function isn't executed right away.

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

Javascript setinterval function with arguments, Use an anonymous function intId = setInterval(function(){waiting(argument)}, 10000);. This creates a parameterless anonymous function which  A protip by bhousman about javascript, settimeout, and setinterval. Let's say you want to pass 2 variables to the function you call in setTimeout.

now with ES5, bind method Function prototype :

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

Reference here

Pass parameters in setInterval function, a function in your code; it's the value undefined , because you assigned it funcName() . Pass Parameters in SetInterval () function in JavaScript. You may perform this work with two ways: pass it directly like described in sample #1 or use an anonymous function like its shown in sample #2.

Add them as parameters to setInterval:

setInterval(funca, 500, 10, 3);

The syntax in your question uses eval, which is not recommended practice.

Javascript setInterval not working, Window setInterval() Method​​ The ID value returned by setInterval() is used as the parameter for the clearInterval() method. Tip: 1000 ms = 1 second. Tip: To execute a function only once, after a specified number of milliseconds, use the setTimeout() method. `INTERVAL` is the delay or interval time we want to wait to fire code. DELAY value is specified in milliseconds which means to wait 3 seconds we should provide 3000 to the setInterval() function. `PARAM1` is optional where it can be used to send parameter to the CODE/FUNCTION; SetInterval() Return Value. After using the setInterval() function it will return a numeric value.

You can pass the parameter(s) as a property of the function object, not as a parameter:

var f = this.someFunction;  //use 'this' if called from class
f.parameter1 = obj;
f.parameter2 = this;
f.parameter3 = whatever;
setInterval(f, 1000);

Then in your function someFunction, you will have access to the parameters. This is particularly useful inside classes where the scope goes to the global space automatically and you lose references to the class that called setInterval to begin with. With this approach, "parameter2" in "someFunction", in the example above, will have the right scope.

Window clearInterval() Method, The setInterval() method can pass additional parameters to the function, as shown in the example below. setInterval(function, milliseconds, parameter1, parameter2, parameter3); The ID value returned by setInterval() is used as the parameter for the clearInterval() method. Instead of executing the function after 1000ms, it will execute it immediately. This is because the compiler first interprets the function calls, like myFun (myParam), and then passes the result as the callback to the setTimeout function. To avoid this, the usual way is to pass function as a variable, thus omitting the parentheses.

     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

Changing the interval of SetInterval while it's running, setinterval. Let's say you want to pass 2 variables to the function you call in setTimeout. Approach 1 var callback = function(a, b){ console.log(a  setTimeout(yourFunctionReference, 4000, param1, param2, paramN); setTimeout will pass all extra parameters to your function so they can be processed there. The anonymous function can work for very basic stuff, but within instance of a object where you have to use "this", there is no way to make it work.

Window setInterval() Method, 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  The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed. The ID value returned by setInterval() is used as the parameter for the clearInterval() method. Tip: 1000 ms = 1 second. Tip: To execute a function only once, after a specified number of milliseconds, use the setTimeout() method.

JavaScript Window Methods Explained with Examples, The function is not passed any arguments, and no return value is expected. code: An optional syntax allows you to include a  The setTimeout function allows you to pass additional parameters to the function. Hence you can rewrite your code as follows: var timer = setTimeout(countDown, 1000, secs); It's supported in all major browsers except for some old versions of IE. See this question for more details: Why does Underscore.js have a delay function?

You can pass arguments into setTimeout and setInterval (Example), Passing a string as a parameter suffers the same hazards as eval(), so it is generally recommended to pass a function pointer instead. msec. Data-type: String. Developers generally pass only two parameters to setTimeout method — the callback function and the timeout period. But in many cases we need to pass custom dynamic data to the callback. This can be easily done by knowing that the setTimeout method can have multiple parameters depending on our need.

Comments
  • Syntax: .setInterval(func, delay[, param1, param2, ...]);
  • what should be for dynamic parameter?
  • @rony36 - you probably want to have a function that creates the interval timer for you. Pass in the parameter to the function so its value is captured in the function closure and retained for when the timer expires. function createInterval(f,dynamicParameter,interval) { setInterval(function() { f(dynamicParameter); }, interval); } Then call it as createInterval(funca,dynamicValue,500); Obviously you can extend this for more than one parameter. And, please, use more descriptive variable names. :)
  • @tvanfosson: awesome answer! Do you know how to clear the interval form within function funca?
  • @tvanfosson Thanks. My bad, but I actually meant the createInterval example you gave in the comment section. How would it work for that? I was thinking of passing a timer variable as a dynamic parameter too, but not sure how or if that makes sense. In the interest of clarity I added a new question here: stackoverflow.com/questions/40414792/…
  • This is the best answer, however it might have unexpected behaviour depending on the function. e.g. console.log.bind(null)("Log me") will throw Illegal invocation, but console.log.bind(console)("Log me") will work as expected. This is because console.log requires console as the this arg.
  • By far the best answer. Lean and clean. Thanks a lot!
  • Very clean and efficient!
  • Just add that works with Chrome>=7, Firefox>=4.0, Explorer>=9, Opera>=11.60, Safari>=5.1 (Source: developer.mozilla.org/ca/docs/Web/JavaScript/Reference/…)
  • Whaa?! Since when was that allowed? (serious question)
  • Not sure. My source was: developer.mozilla.org/en/DOM/window.setInterval
  • @Kev Internet Explorer is a real mess It does not supports passing arguments -.-
  • IMHO this should be the accepted answer. Nice, simple and clean solution.