admin管理员组

文章数量:1304184

function updateimage(){
 $("#fileimg").attr("src","secondimage.jpg");
 $('#fileimg').fadeIn('slow');
}
setTimeout(updateimage(), 5000);

This is the code i tried. Its a code to reload the image every 5 seconds. But it doesn't work. I get this error in IE: Invalid argument Can y'all help me? Thanks.

function updateimage(){
 $("#fileimg").attr("src","secondimage.jpg");
 $('#fileimg').fadeIn('slow');
}
setTimeout(updateimage(), 5000);

This is the code i tried. Its a code to reload the image every 5 seconds. But it doesn't work. I get this error in IE: Invalid argument Can y'all help me? Thanks.

Share Improve this question edited Jan 21, 2011 at 21:01 Daniel Huckstep 5,40810 gold badges42 silver badges56 bronze badges asked Dec 2, 2010 at 17:50 ThewThew 16k19 gold badges61 silver badges102 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 10

You should pass the actual function as argument and not the call:

setTimeout(updateimage, 5000);

2 options:

setTimeout("updateimage()", 5000)  

or use a function:

setTimeout(function() {
        updateimage();
}, 5000);

Try

setTimeout('updateimage()', 5000);

According to the microsoft documentation here it the parameter has to be either a function pointer or a string. So both the twerks below will work.

Method 1

setTimeout(updateimage, 5000);

Method 2

setTimeout("updateimage", 5000);
setTimeout(updateimage(), 5000);

Remove the parenthesis from updateimage, so it is:

setTimeout(updateimage, 5000);

As others have stated you are calling it wrong.

What you have there:

function updateimage(){
 $("#fileimg").attr("src","secondimage.jpg");
 $('#fileimg').fadeIn('slow');
}

setTimeout(updateimage(), 5000);

When executed this will pass the result of updateImage() to the setTimeout() call. As your function returns no value, you are in effect actually saying:

setTimeout(null, 5000);

Pass the function by its name, as if it were a variable of that name, which indeed it is.

setTimeout(updateimage, 5000);

本文标签: javascriptTimeout doesn39t workStack Overflow