Timeout doesn't work
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 w开发者_运维知识库ork. I get this error in IE: Invalid argument Can y'all help me? Thanks.
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);
精彩评论