HTML and CSS Reference
In-Depth Information
Recursion
In this chapter's project,
recursion is used to
keep a routine going
indefinitely or until some
other function is called
to stop it. Normally, a
recursive function should
have a mechanism that
terminates the function
when it completes its task.
Using the setTimeout() Method to Create a Recursive Call To have the
message text scroll continuously in the text field, you use a programming technique called
recursion , in which a function is called within itself, creating an “endless loop.” The
setTimeout() method is used to call a function or evaluate an expression after a specified
amount of time has elapsed, which is measured in milliseconds. The general form of the
setTimeout() method is shown in Table 10-7.
Table 10-7 setTimeout() Method
General form:
setTimeout(“instruction”, time delay in milliseconds)
Comment:
where instruction is any valid JavaScript statement and time delay is expressed in number of
milliseconds
Example:
window.setTimeout(“scrollingMsg()”,200)
The recursive call of the scrollingMsg() user-defined function by the setTimeout()
method continuously displays characters and regulates the speed of the characters
displaying in the text field.
To Add the setTimeout() Method to Create a Recursive Call
The following step adds the setTimeout() method to create a recursive call to the scrollingMsg() function, which
will control how fast the characters in the scrolling message are displayed.
1
line 16 and indent
under the previous
line of code, type
window.setTimeout
(“scrollingMsg()”,
200) to call
scrollingMsg() from
within itself, and then
press the e n t e r key.
If necessary, click
setTimeout recursive call
every 200 milliseconds
end of function
point under
the function
statement, type } on
line 17 to close the
function, and then press the e n t e r key two times (Figure 10-10).
How do you know how fast to make the scrolling?
The best way is to try several different speeds and ask potential
users to look at it and indicate their preference.
Align the insertion
Figure 10-10
What if you changed the number from 200 to 2000?
The text would display one character every two seconds and would be
too boring to watch, you would lose the interest of your user.
Search WWH ::




Custom Search