下面的代碼是從「通過實例第二版的JavaScript」,我認爲下面的代碼是更好setTimeout和setInterval哪個更好?
function scroller() {
str = str.substring(1, str.length) + str.substring(0, 1);
document.title = str;
window.status = str;
}
setInterval(scroller, 300);
的舊代碼是遞歸的,並會繼續下去,直到節目結束調用自身每0.3秒,我想舊的代碼可能會導致堆棧溢出,對吧?
<html>
<!-- This script is a modification of a free script found at
the JavaScript source.
Author: Asif Nasir ([email protected])
-->
<head>
<script type="text/javascript">
var today = new Date();
var year = today.getFullYear();
var future = new Date("December 25, " + year);
var diff = future.getTime() - today.getTime();
// Number of milliseconds
var days = Math.floor(diff/(1000 * 60 * 60 * 24));
// Convert to days
var str =
"Only " + days + " shopping days left until Christmas!";
function scroller() {
str = str.substring(1, str.length) + str.substring(0, 1);
document.title = str;
window.status = str;
setTimeout(scroller, 300); // Set the timer
}
</script>
</head>
<body onload="scroller()">
<b>
<font color="green" size="4">
Get Dizzy. Watch the title bar and the status bar!!
<br />
<image src="christmasscene.bmp">
</font>
</body>
</html>
他們是不同的功能......真正的問題是什麼? – jeremy
什麼是「更好」的標準? – RobG