开发者

Javascript setTimeout ignoring time parameter

开发者 https://www.devze.com 2023-04-12 04:59 出处:网络
It\'s not the first time I\'ve used setTimeout(), but I can\'t figure out what the problem is. The code part of the setTimeout() is executing cor开发者_Python百科rectly, but it is executing immediatel

It's not the first time I've used setTimeout(), but I can't figure out what the problem is. The code part of the setTimeout() is executing cor开发者_Python百科rectly, but it is executing immediately without the delay. If anyone can see the problem, that would help. Here's the code:

if(token==1){
    img1.src=ssImages[imgNum];
    num1=0;
    num2=10;
    setTimeout('crossFade()',2500);
}


Are you sure this is the code? If it executes immediately there are usually two reasons:

  1. The developer thought the time is specified in seconds - but 2500 is fine, that's 2.5 seconds.
  2. He calls the function immediately (e.g. setTimeout(foo(), 1234));

But none of the reasons apply to your code so check the rest of the code if there are any other calls to that function.


Anyway, you should really pass a function instead of a string:

setTimeout(crossFade, 2500);

Or, if you need to specify any arguments:

setTimeout(function() {
    crossFade(...);
}, 2500);


I agree with Theifmaster. The window. setTimeout method takes two arguments:

1) Function OR Expression 2) Time in ms

In your code you provide a string or an Expression :

setTimeout('crossFade()',....)

This is generally discouraged as with the use of eval. You should pass a function - either named:

setTimeout(crossFade,....)

OR as suggested anonymous:

setTimeout(function(){crossFade()},....

This is about all you can do to trouble shoot this code unless you provide an example ok jsfiddle for us to see the context this is called.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号