Skip to Content
Head Rush Ajax
book

Head Rush Ajax

by Brett McLaughlin
March 2006
Beginner to intermediate
448 pages
13h 33m
English
O'Reilly Media, Inc.
Content preview from Head Rush Ajax
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
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Become a Game-Changing Leader

How to Become a Game-Changing Leader

Douglas A. Ready, Alan Mulally

Publisher Resources

ISBN: 0596102259Errata PageSupplemental Content