为什么这个 javascript 函数没有定义?
我有这个 javascript 代码:
$(function(){
var currentCarouselItem = 1; //set carousel to first slide
var runCarousel = 1;
$(window).load(function(){
setTimeout('autoScroll()', 10000);
});
function autoScroll(num){
if (runCarousel == 1) {
$('.carouselItem.' + currentCarouselItem).animate({left: '975px'}, 'slow', function(){
$(this).removeClass('active');
$(this).attr('style','');
var nextItem = currentCarouselItem + 1;
if (nextItem == 7) {
nextItem = 1;
}
$('.carouselItem.' + nextItem).animate({left: '110px'}, 'slow', function(){
$(this).addClass('active');
})
})
}
}
})
每当我运行该网站时,它都会抛出控制台错误: Uncaught ReferenceError: autoScroll is not Defined
知道为什么它认为它没有定义吗?
I have this javascript code:
$(function(){
var currentCarouselItem = 1; //set carousel to first slide
var runCarousel = 1;
$(window).load(function(){
setTimeout('autoScroll()', 10000);
});
function autoScroll(num){
if (runCarousel == 1) {
$('.carouselItem.' + currentCarouselItem).animate({left: '975px'}, 'slow', function(){
$(this).removeClass('active');
$(this).attr('style','');
var nextItem = currentCarouselItem + 1;
if (nextItem == 7) {
nextItem = 1;
}
$('.carouselItem.' + nextItem).animate({left: '110px'}, 'slow', function(){
$(this).addClass('active');
})
})
}
}
})
Whenever I run the site it throws a console error: Uncaught ReferenceError: autoScroll is not defined
Any idea why it thinks it is not defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么要把它放在引号中?
这是对于初学者来说的。
此外,这里还存在范围界定问题。
Why put it in quotes?
That's for starters.
Additionally, you have scoping issues here.
我可以尝试为你回答,但我认为这个人做得更好:
JQuery,setTimeout 不起作用
I could try answering it for you, but I think this guy does a lot better job:
JQuery, setTimeout not working
我认为这是因为您的
autoScroll
函数位于最外层$(function(){})
创建的闭包内。因此 eval(用于评估 setTimeout 中的字符串)无法找到它,因为它在“全局”范围内运行。您可以将
autoScroll
的定义移到外部。另外,正如jcolebrand建议的那样,删除引号。
I think this is because your
autoScroll
function is inside closure created by outermost$(function(){})
. Therefore eval (used to evaluate your string insetTimeout
) can't find it, as it runs in a 'global' scope.You can move the definition of
autoScroll
outside.Also, as jcolebrand suggested, remove quotes.
我认为这是因为当您传入一个字符串作为
setTimeout()
的第一个参数时,javascript 基本上从该字符串的全局范围运行eval()
。autoScroll
存在于$(function() { })
的范围内,因此无法从全局范围“看到”。尝试将其更改为
setTimeout(autoScroll, 10000);
I think it is because when you pass in a string as the first argument for
setTimeout()
that javascript basically runseval()
from the global scope on that string.autoScroll
lives within the scope of$(function() { })
and therefore can't be "seen" from the global scope.Try changing it to
setTimeout(autoScroll, 10000);
您的代码存在一些问题,但未定义
autoScroll
函数的原因是它在文档就绪函数的范围内定义,但通过eval
执行> 在文档准备好超出范围而没有正确关闭之后。There a couple of problems with your code, but the reason that the
autoScroll
function is not defined is that it defined within the scope of your document ready function, but is executed viaeval
after the document ready has gone out of scope without the proper closure.对于初学者来说,您需要在这样的函数末尾添加一个分号,
For starters you need a semi colon at the end of functions like this,