JavaScript 函数有时未定义
我相信以前已经有人问过这个问题,但尚未确定具体答案。
在我的网站http://euphoricsoftware.com/上有一个奇特的倒计时脚本可以自动执行您可以访问正常站点,而不是移动站点或低带宽站点。该脚本适用于 (其中 8 是起始数字)的所有浏览器,但有时在 Chrome 中倒计时不会不动,打开 JS 控制台会显示
Uncaught ReferenceError: countDown is not Defined
。
页面上还有一个按钮,可让您暂停和恢复倒计时。 Resuming 也会调用 countDown()
函数,即使 onload 发生未定义的错误,如果单击按钮两次倒计时也会起作用,所以这似乎与 onload 有关。
这是我一直在使用的代码(SO 的代码格式有点塞满了间距):
<html>
<head>
<!-- ... -->
<script type="text/javascript">
var stopRedirect = false;
var back = 0;
function redirect()
{
if (!stopRedirect) {window.location = "home.html";}
}
function countDown(num)
{
if (!stopRedirect)
{
back = num-1;
if (num < 10)
{
document.getElementById("top").innerHTML=num+1;
}
document.getElementById("middle").innerHTML=num;
if (num > 1)
{
document.getElementById("bottom").innerHTML=num-1;
var t = setTimeout("countDown("+(num-1)+")",1000);
}
else
{
document.getElementById("bottom").innerHTML=" ";
document.getElementById("unit").innerHTML=" second ";
var r = setTimeout("redirect()",1000);
}
}
}
function stop()
{
if (!stopRedirect)
{
stopRedirect = true;
document.getElementById("stop").style.display="none";
document.getElementById("start").style.display="block";
}
}
function start()
{
if (stopRedirect)
{
stopRedirect = false;
document.getElementById("stop").style.display="block";
document.getElementById("start").style.display="none";
var c = setTimeout("countDown("+(back)+")",1000);
}
}
</script>
</head>
<body onLoad="setTimeout(countDown(8),1000);">
<!-- ... -->
您可以在 上查看该网站的运行情况http://euphoricsoftware.com/
有谁知道为什么会发生这种情况?谢谢
I believe this has been asked before but no concrete answer has been determined.
On my website http://euphoricsoftware.com/ there is a fancy countdown script to automatically take you to the normal site, as opposed to mobile or low bandwidth. The script works in every browser with <body onload="setTimeout(countDown(8),1000);">
(where 8 is the number to start from) except sometimes in Chrome the countdown doesn't move and opening the JS console reveals Uncaught ReferenceError: countDown is not defined
.
Also on the page is a button which lets you pause and resume the countdown. Resuming calls the countDown()
function, too, and even when the undefined error happens onload, if you click the button twice the countdown will work, so it seems to be something to do with onload.
Here's the code I've been using (SO's code format has stuffed up the spacing a bit):
<html>
<head>
<!-- ... -->
<script type="text/javascript">
var stopRedirect = false;
var back = 0;
function redirect()
{
if (!stopRedirect) {window.location = "home.html";}
}
function countDown(num)
{
if (!stopRedirect)
{
back = num-1;
if (num < 10)
{
document.getElementById("top").innerHTML=num+1;
}
document.getElementById("middle").innerHTML=num;
if (num > 1)
{
document.getElementById("bottom").innerHTML=num-1;
var t = setTimeout("countDown("+(num-1)+")",1000);
}
else
{
document.getElementById("bottom").innerHTML=" ";
document.getElementById("unit").innerHTML=" second ";
var r = setTimeout("redirect()",1000);
}
}
}
function stop()
{
if (!stopRedirect)
{
stopRedirect = true;
document.getElementById("stop").style.display="none";
document.getElementById("start").style.display="block";
}
}
function start()
{
if (stopRedirect)
{
stopRedirect = false;
document.getElementById("stop").style.display="block";
document.getElementById("start").style.display="none";
var c = setTimeout("countDown("+(back)+")",1000);
}
}
</script>
</head>
<body onLoad="setTimeout(countDown(8),1000);">
<!-- ... -->
and you can see the site in action at http://euphoricsoftware.com/
Does anyone know why this is happening? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定为什么会收到该错误,因为您的函数是在
中定义的,并且在
onload
之前您不会尝试使用它身体,但你的代码确实有问题。onload=""
中的这一部分:当
onload
发生并且代码运行时,将调用countDown()
函数立即,传递参数8
,然后获取该函数返回的任何内容并将其传递给setTimeout()
以在1秒内执行。在您的情况下,您的函数不会返回特定值,因此实际上您将undefined
传递给setTimeout()
。您想要做的是将函数引用或字符串传递给 setTimeout()。
您不能在传递该函数的参数的同时直接传递对
countDown()
的引用(至少不能使用setTimeout()
的语法将在 IE 中工作),因此您需要将其包装在一个匿名函数中,如下所示:或者您可以使用类似于
countDown()
函数体内的字符串格式(使用单引号,因为当前使用的onload
属性doubles):请注意,字符串格式通常不受欢迎,因为它速度较慢并且会影响范围。
I'm not quite sure why you get that error, given that your function is defined in the
<head>
and you don't try to use it until theonload
of the body, but your code does have a problem. This part from youronload=""
:will, when the
onload
occurs and the code is run, call thecountDown()
function immediately, passing a parameter of8
, and then take whatever that function returns and pass it tosetTimeout()
to be executed in 1 second's time. In your case your function doesn't return a particular value, so in effect you are passingundefined
tosetTimeout()
.What you want to do is pass
setTimeout()
either a function reference or a string.You can't pass a reference to
countDown()
directly at the same time as passing a parameter for that function (at least, not with a syntax ofsetTimeout()
that will work in IE), so you would need to wrap it in an anonymous function like this:Or you can use the string format similar to within your
countDown()
function body (using single-quotes since theonload
attribute currently uses doubles):Note that the string format is generally frowned upon because it is slower and affects the scope.