There are two different scenarios with multiple requests that you have to
plan for:
CHAPTER 5 n AJAX AND DATA EXCHANGE 122
??? An initial request is made, but then a subsequent call is made that should override the
first call. For example, a user fills out a search box and then presses Enter. But before
the call has returned, the user realizes that a mistake was made, corrects it, and presses
Enter again. The user doesn??™t want the first set of results??”only the second. You??™ll need
to detect that a second request has been made and determine whether you need to
override the original request.
??? The other scenario is when you make sequential calls, but the calls return out of order.
For example, you have a chat program that continually polls the server for new messages.
Messages need to be returned in the same order.
If you need to keep your calls in order, essentially mimicking a synchronous system with
an asynchronous one, you need to keep track of each call through a token. The token could
just be an integer that you increment each time you make a call.
Then you process the callbacks only when the current token is the next valid token; or if
a record or two is skipped, wait until the missing record comes in or times out.
Unexpected Data
You should never assume what data comes back from the server. You??™ve already got an onerror
handler in case the server throws back something unusual.
Pages:
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191