Take the 2-minute tour ×

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):

    <!-- ... -->
    <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)
            if (num > 1)
                var t = setTimeout("countDown("+(num-1)+")",1000);
                document.getElementById("unit").innerHTML=" second&nbsp;";
                var r = setTimeout("redirect()",1000);

    function stop()
        if (!stopRedirect)
            stopRedirect = true;
    function start()
        if (stopRedirect)
            stopRedirect = false;
            var c = setTimeout("countDown("+(back)+")",1000);
<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

share|improve this question
I can't see why this would not work. I refreshed your page in Chrome about a hundred times and never had a problem or saw an error. –  mrtsherman Jan 29 '12 at 4:38
in both Chrome 18.0.1017.2 dev-m and 18.0.1021.0 canary it doesn't seem to work –  ProfSmiles Jan 29 '12 at 4:49
Works fine for me in Canary. –  mrtsherman Jan 29 '12 at 5:11
I had changed it to nnnnnn's way and I've removed the timeout and it seems to be working fine –  ProfSmiles Jan 29 '12 at 5:15
add comment

1 Answer

up vote 1 down vote accepted

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="":


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.

share|improve this answer
the string works perfectly however the anonymous function still comes up with Uncaught ReferenceError: countDown is not defined –  ProfSmiles Jan 29 '12 at 4:50
Sorry, like I said in my first sentence I can't explain that error. It doesn't happen for me, but my version of Chrome is 16.something. By the way, I'm not sure that you even need setTimeout in the onload, you could just say onload="countDown(9);". –  nnnnnn Jan 29 '12 at 5:12
add comment

Your Answer


