you’re on your way 4
55
next generation applications
function updatePage() {
if (request.readyState == 4) {
var newTotal = request.responseText;
var boardsSoldEl = document.getElementById(“boards-sold”);
var cashEl = document.getElementById(“cash”);
replaceText(boardsSoldEl, newTotal);
/* Figure out how much cash Katie has made */
...
/* Update the cash total on the web form */
...
}
}
Here’s
that code
you wrote
a few
pages back.
When the request object’s
readyState is set to 4, the
server is done with the request.
Don’t forget the closing bracket.
You already know that the browser will run your updatePage() function when it gets a response
from the server. But, there’s a twist: the browser actually runs updatePage() every time the ready
state changes, and not just when the server is nished sending back a response. Look at the diagram
on page 54 again, and notice that as the request is progressing, the ready state number changes.
It looks like when the server is nished with our request, the ready state is “4”, so we can check for
that number in our code. That way, we can make sure that we only try and update Katie’s report
when we know that the server has sent us back a response.
Checking for the right ready state
readyState is the property on the request
object that stores the current ready state.
This function
will get run
every time
the ready
state changes.
When the serv