javascript setTimeout 的问题“函数未定义”!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3755709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem with setTimeout "function is not define" !
提问by faressoft
problem with setTimeout "function is not define" !
setTimeout 的问题“函数未定义”!
What is the problem in this code ?
这段代码有什么问题?
$(document).ready(function(){
function ISS_NextImage() { //ImageSlideShow NextImage
$('.ImageSlideShow').each(function() {
alert($(".correntImage", this).text());
});
}
var t=setTimeout("ISS_NextImage();",1000);
});
回答by Quentin
When you evalcode, it is done in the global scope. Since the function you are trying to call is locally scoped, this fails.
当您eval编码时,它是在全局范围内完成的。由于您尝试调用的函数是本地范围的,因此失败。
Pass the function to setTimeoutinstead of passing a string to be evaled.
将函数setTimeout传递给而不是传递要eval编辑的字符串。
var t=setTimeout(ISS_NextImage,1000);
回答by mcgregok
Try changing your set timeout call to this:
尝试将您的设置超时调用更改为:
var t=setTimeout(function(){ISS_NextImage();},1000);
回答by istruble
Avoid passing a string to setTimeout(). Just pass a reference to the function instead:
避免将字符串传递给 setTimeout()。只需传递对函数的引用:
var t = setTimeout(IIS_NextImage, 1000);
回答by Ryan Ternier
you could also:
你也可以:
$(function() {
var t = setTimeout(new function() {
$('.ImageSlideShow').each(function() {
alert($(".correntImage", this).text());
});
}, 1000);
});
回答by Joris Kok
You could do something like this:
你可以这样做:
$(document).ready(function(){
setTimeout(ISS_NextImage,1000);
});
function ISS_NextImage() {
$('.ImageSlideShow').each(function() {
alert($(".correntImage", this).text());
});
}

