0

I have this chunk of javascript that's kind of hacked around from http://www.developphp.com/view.php?tid=1248 and I am seeing an error of "undefined variable - broadcast".

function cdtd(broadcast) {
    /* expected date format is Month DD, YYYY HH:MM:SS */
    var nextbroadcast = new Date(broadcast);
    var now = new Date();
    var timeDiff = nextbroadcast.getTime() - now.getTime();
    if (timeDiff <= 0) {
        clearTimeout(timer);
        document.getElementById("countdown").innerHTML = "<a href=\"flconlineservices.php\">Internet broadcast in progress<\/a>";
        /* Run any code needed for countdown completion here */
    }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %= 60;
    document.getElementById("daysBox").innerHTML = days + " d";
    document.getElementById("hoursBox").innerHTML = hours + " h";
    document.getElementById("minsBox").innerHTML = minutes + " m";
    // seconds isn't in our html code (javascript error if this isn't commented out)
    /*document.getElementById("secsBox").innerHTML = seconds + " s";*/
    var timer = setTimeout('cdtd(broadcast)',1000);
}

"broadcast" is passed from the page with this <script type="text/javascript">cdtd("<?php echo $nextbroadcast; ?>");</script>. $nextbroadcast is based upon the date/time when the user views the page.

I tried var broadcast;, var broadcast = "";, and var broadcast = null;. Whenever I try to declare the variable, before the function, it breaks the script.

Am I doing something incorrectly? The script is working, just fine, but I'd rather not have the error.

4

2 回答 2

2

这可能是问题所在:

var timer = setTimeout('cdtd(broadcast)',1000);

您应该声明var timer;上面的cdtd()函数,然后在函数下方外部设置它:

var func = 'cdtd(' + broadcast + ')';

timer = setTimeout(func,1000);
于 2012-07-03T22:04:36.573 回答
2

更改以下行:

var timer = setTimeout('cdtd(broadcast)',1000);

对此:

var timer = setTimeout(function() { cdtd(broadcast); }, 1000);

于 2012-07-03T22:04:54.360 回答