function in setInterval() executes without delay

前端 未结 4 681
广开言路
广开言路 2020-12-02 00:49

I am in the process of making a jquery application to hide an image after a specified interval of time by using setInterval(). The problem is that the hide image function ex

4条回答
  •  悲哀的现实
    2020-12-02 01:41

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

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

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

    setInterval(change, 9999999);
    

    Either works. I personally find the first one a bit clearer about the intent than the second.

提交回复
热议问题