The function in setInterval () is executed without delay

I am making a jquery application to hide an image after a certain period of time using setInterval (). The problem is that the latent image function is executed immediately without delay.

$(document).ready(function() { setInterval(change(), 99999999); function change() { $('#slideshow img').eq(0).removeClass('show'); } }); 

I am testing it in jsfiddle .

+4
source share
4 answers

http://jsfiddle.net/wWHux/3/

You called the function right away, not passed it to setInterval .

setInterval( change, 1500 ) - passes the change function to setInterval

setInterval( change(), 1500 ) - calls the change function and passes the result ( undefined ) to setInterval

+10
source

If you have setInterval(change(), 99999999); , you end up calling the change() function and pass the return value to its setInterval() function. You need to delay the execution of change() by wrapping it in a function.

 setInterval(function() { change() }, 9999999); 

Or you can delay it by passing setInterval() only to the function itself, without calling it.

 setInterval(change, 9999999); 

Or it works. I personally find the first, a little clearer about the intention, than the second.

+4
source

You have setInterval(change(), 99999999); , and it should be setInterval(change, 99999999); . See the documentation for setInterval / setTimeout. :)

A common mistake happens to me all the time. :)

+1
source

Change setInterval(change(), 99999999); on setInterval(change, 99999999);

And 99999999 means 99999999 milliseconds, as you know.

+1
source

All Articles