106
Chapter 2
Man, Break Neck pizza rocks. There’s
nothing like a breakfast of cold pizza, a
whipped latte from Starbuzz, and the
Webville Times to read.
<script>
var request...
function foo()
{
...
}
</script>
JavaScript
<script>
var request...
function foo()
{
...
}
</script>
Customer’s Phone Number
Customer’s Address
Customer’s Order
As long as you remember the name of the
property you use to set the callback function,
you’ll remember that the callback is run more
than one time. Do you remember what the
property was called?
It’s onreadystatechange. So when the
ready state changes from 1 to 2, for example,
your callback function, updatePage(), gets
run. In other words, updatePage() will run
several times: when the ready state changes from
1 to 2, when it changes again from 2 to 3, and
one more time, when the ready state changes
from 3 to 4.
But the server only guarantees that it’s got data
you can use when the ready state is 4. So you
need to check the current ready state before
trying to update the order form, or the page
might end up with bad or missing data.
Right—because updatePage() runs
every time the ready state changes.
So that’s why we check to make sure the ready state
is 4 before doing anything in our callback function,
right? Otherwise, our JavaScript might try and
update the page before the server is nished.
ready states and callback functions