Skip to content Skip to sidebar Skip to footer

Why Cant I Invoke A Function With Paremeters Multiple Time In Settimeout/setinterval

I am trying to invoke a function in my setinterval function like this function dosomething(variable) { console.log(variable); } setinterval(dosomething(variable), 2000) The abov

Solution 1:

You can use a anonymous function which will call the dosomething method

setInterval(function(){dosomething(variable)}, 2000)

In your case you are calling dosomething and then is passing the value returned by it to the setInterval()

Solution 2:

This line

setinterval(dosomething(variable), 2000)

is the same as

var x = dosomething(variable);
setinterval(x, 2000);

Using the debugger you will notice x is undefined

so you get

dosomething(variable);
setinterval(undefined, 2000);

Hence running once and straight away

Post a Comment for "Why Cant I Invoke A Function With Paremeters Multiple Time In Settimeout/setinterval"