Here the timer goes off at intervals of 100 milliseconds (ten times a second)
because until the page unloads, you don’t use the stopTimerfunction — and
also because you continually re-execute the movepfunction. This function
changes the text’s font color from black and blue and back again, creating a
subtle, but disturbing, throbbing quality. Don’t stare at it too long or you
might freak yourself out.
The following list shows the major events that happen as you run the preced-
ing code example:
- When a Web page contains a section of script, any lines of program-
ming that aren’t enclosed in a Function... End Function
(or Sub... End function) are executed immediately when the
page loads.
In other words, in the preceding example, the line dim toggleis outside
any Functionor Sub. Therefore, the variable toggleis created when this
page first loads into a browser. (The dimcommand creates a variable.)
The main value of using the dimcommand to create a variable outside
of any Functionor Subis that the variable can then be used by all the
Functionsand Subsanywhere in the current Web page. (Variables cre-
ated withina Functionor Subcan be used only by other lines of source
code within that singleFunctionor Sub.)
In this example, you wanted to have a variable, toggle, that could hold
information no matter what function was, or was not, currently executing.
Because you create toggleoutside the Functions, it can hold the infor-
mation you give it as long as this page remains in the browser.
- When the startTimerfunction is executed (with the body onload
command), the browser is told to execute the movepfunction every
80 milliseconds.
Note that this execution happens quite often because 1000 milliseconds
is one second. - The meat of this program is in the movepfunction. The line toggle =
not toggleis like flipping a light switch.
If the variable togglewas true, it becomes false when you use the not
command. If it was false, it becomes true. - The variable toggleis tested. If it’s true (if toggle then), the para-
graph is displayed blue. If toggleis false (else), the paragraph turns
black. - The movepfunction continues to toggle on and off, quite rapidly, until
the stopTimerfunction is executed when the browser unloads this
page, triggering the onunload=”stopTimer()”command.
You can find lots of scripts ready to copy and paste on the Internet. Consult
some of the resources listed in Chapters 18 and 19.