-1

The following code is from 'JavaScript by Example Second Edition',I think the code below is better

function scroller() {
    str = str.substring(1, str.length) + str.substring(0, 1);
    document.title = str;
    window.status = str;
}
setInterval(scroller, 300);

The old code is recursive and will continue to call itself every 0.3 seconds until the program ends, I think the old code maybe cause stack overflow, right?

<html>
<!-- This script is a modification of a free script found at
the JavaScript source.
Author: Asif Nasir (Asifnasir@yahoo.com)
-->
<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>
HelloCW
  • 843
  • 22
  • 125
  • 310

2 Answers2

1

Have a look here:

'setInterval' vs 'setTimeout'

setTimeout runs the code/function once after the timeout.

setInterval runs the code/function in intervals, with the length of the timeout between them.

For what you're doing, you should be using setInterval.

Community
  • 1
  • 1
Chase
  • 29,019
  • 1
  • 49
  • 48
  • I usually like to use a "re-prime" with setTimeout; it can avoid having to keep track of the timer ID if something needs to be cancelled. –  Oct 12 '12 at 02:38
1

setInterval is good if you don't care too much about accuracy, e.g. polling for some condition to be met.

setTimeout is good if you want a one–off event or need to adjust the interval between calls, e.g. a clock that should update as close as possible to just after the next whole second.

Both can be used for events that run continuously at approximately the specified interval, both can be cancelled, both only run at about (as soon as possible after) the designated time interval.

Incidentally, the first code example in the OP should not cause a stack overflow, though it is otherwise not very well written.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks! In other computer languages, recursive function maybe cause stack overflow, why don't Javascript cause stack overflow when it keep running? – HelloCW Oct 12 '12 at 02:41
  • 2
    @user828896 *Neither* `setTimeout` nor `setInterval` will cause a recursive stack overflow. The stack is effectively unwound at the Event Dispatcher (from which the callbacks are invoked). –  Oct 12 '12 at 02:45
  • @user828896—`setTimeout` and `setInterval` aren't recursive in the typical sense, they are put on a stack and executed. You can end up with a huge scope chain created by closures, but there aren't any here and modern script engines should be able to deal with that fairly easily. Both functions have been around a long time so should be well optimised by now. – RobG Oct 12 '12 at 06:28