Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters in setInterval function

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

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

like image 308
Rakesh Avatar asked Jan 19 '09 14:01

Rakesh


People also ask

How do you pass parameters to a function in setInterval?

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.

What are the parameters to be passed to setTimeout and setInterval?

The first parameter is required and is the callback function to be executed. The second parameter is optional and represents the number of milliseconds to wait before invoking the callback function.

How does the setInterval () function work in?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .

How does a setInterval function return a value?

function git(limit) { var i = 0; var git = setInterval(function () { console. log(i); if (i === limit - 1) { clearInterval(git); return 'done'; } i++; }, 800); } var x = git(5); console. log(x);


2 Answers

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

setInterval( function() { funca(10,3); }, 500 ); 
like image 58
tvanfosson Avatar answered Sep 20 '22 04:09

tvanfosson


Add them as parameters to setInterval:

setInterval(funca, 500, 10, 3); 

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

like image 25
Kev Avatar answered Sep 19 '22 04:09

Kev