JavaScript 函数有时未定义

发布于 2024-12-29 08:07:20 字数 2272 浏览 0 评论 0原文

我相信以前已经有人问过这个问题,但尚未确定具体答案。

在我的网站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="&nbsp;";
                document.getElementById("unit").innerHTML=" second&nbsp;";
                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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

节枝 2025-01-05 08:07:20

我不太确定为什么会收到该错误,因为您的函数是在 中定义的,并且在 onload 之前您不会尝试使用它身体,但你的代码确实有问题。 onload="" 中的这一部分:

setTimeout(countDown(8),1000);

onload 发生并且代码运行时,将调用 countDown() 函数立即,传递参数8,然后获取该函数返回的任何内容并将其传递给setTimeout()以在1秒内执行。在您的情况下,您的函数不会返回特定值,因此实际上您将 undefined 传递给 setTimeout()

您想要做的是将函数引用或字符串传递给 setTimeout()。

您不能在传递该函数的参数的同时直接传递对 countDown() 的引用(至少不能使用 setTimeout() 的语法将在 IE 中工作),因此您需要将其包装在一个匿名函数中,如下所示:

onload="setTimeout(function() { countDown(8); }, 1000);"

或者您可以使用类似于 countDown() 函数体内的字符串格式(使用单引号,因为当前使用的 onload 属性doubles):

onload="setTimeout('countDown(8);', 1000)"

请注意,字符串格式通常不受欢迎,因为它速度较慢并且会影响范围。

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 the onload of the body, but your code does have a problem. This part from your onload="":

setTimeout(countDown(8),1000);

will, when the onload occurs and the code is run, call the countDown() function immediately, passing a parameter of 8, and then take whatever that function returns and pass it to setTimeout() to be executed in 1 second's time. In your case your function doesn't return a particular value, so in effect you are passing undefined to setTimeout().

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 of setTimeout() that will work in IE), so you would need to wrap it in an anonymous function like this:

onload="setTimeout(function() { countDown(8); }, 1000);"

Or you can use the string format similar to within your countDown() function body (using single-quotes since the onload attribute currently uses doubles):

onload="setTimeout('countDown(8);', 1000)"

Note that the string format is generally frowned upon because it is slower and affects the scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文