HTML and CSS Reference
In-Depth Information
Giving requests a timeout
A request to a remote server may take a while to complete. The time-to-response depends on a
number of factors, including bandwidth, and traffic on the target site. If completing the operation in
a fixed amount of time is critical for your application, you might want to associate a timeout with your
WinJS.xhr
request. When you do that, if the request is still pending after the specified amount of time
elapses, the request is automatically canceled and raises an error that propagates back to the calling
application. Here's the code you need to set up a timeout:
WinJS.Promise.timeout(3000,
WinJS.xhr({ url: ... }).then(
function (response) {
// Process response
},
function (error) {
rssReaderApp.alert(error.message);
})
);
Basically, all you do is wrap your
WinJS.xhr
call within a call to
WinJS.Promise.timeout
. The first
parameter sets the expected timeout (in milliseconds); the second parameter is your call to
WinJS.xhr
.
Note that if the request fails because it times out, then the error object you receive has the
message
property set to a generic message you can display directly to users, in this case, just “Canceled”
(see Figure 11-3).
FIGURE 11-3
A timed-out request.
Search WWH ::
Custom Search