最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - function in setInterval() executes without delay - Stack Overflow

programmeradmin0浏览0评论

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 executes immediately without delay.

$(document).ready(function() {

  setInterval(change(), 99999999);

  function change() {
    $('#slideshow img').eq(0).removeClass('show');

  }

});

I am testing it in jsfiddle.

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 executes immediately without delay.

$(document).ready(function() {

  setInterval(change(), 99999999);

  function change() {
    $('#slideshow img').eq(0).removeClass('show');

  }

});

I am testing it in jsfiddle.

Share Improve this question edited Nov 9, 2016 at 14:19 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Oct 22, 2011 at 8:17 user701510user701510 5,76317 gold badges62 silver badges86 bronze badges 1
  • 1 setInterval(change(), x); -> setInterval(change, x); – Felix Kling Commented Oct 22, 2011 at 8:19
Add a comment  | 

4 Answers 4

Reset to default 11

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

You called the function immediately instead of passing it to setInterval.

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

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

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.

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

Common mistake, happens to me all the time. :)

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

And 99999999 means 99999999 milliseconds as you known.

发布评论

评论列表(0)

  1. 暂无评论