AJAX - The Complete Reference

(avery) #1

PART II


Chapter 5: Developing an Ajax Library 219


makes the request every two seconds. A decay value can also be specified. This multiplies the
wait time by the provided value every time a request is made where the contents are the same
as the last request.

var updater = new Ajax.PeriodicalUpdater("responseOutput",
"http://ajaxref.com/ch5/sayhello.php",
{ method: "GET", frequency: 3,
decay: 2});

For example, in the preceding code snippet, a decay value of 2 with a frequency
of 3 seconds would back off first with 6 seconds, then 12 seconds, then 24 and so on. You
can see that decay can be helpful but also troubling if the value gets large. An example
of using the periodical updated concept shown here can be found at
http://ajaxref.com/ch5/prototypeperiodicalupdater.html.

One interesting aspect of Prototype’s Ajax offering is the use of global responders. By
calling Ajax.Responders.register(),a function can be associated with any request state
for all future requests. For example, in this code snippet, a call to logRequest() and
logResponse() are registered to the onCreate and onComplete states of any Ajax request.

Ajax.Responders.register({
onCreate: logRequest,
onComplete: logResponse
});

These functions below are being used in the simple example found at
http://ajaxref.com/ch5/prototyperesponder.html and shown in the following illustration
to show the success or failure of various requests.

function logResponse(response)
{
$("log").innerHTML += "<strong>Response:</strong>" + response.url + " - " +
response.transport.status + "<br />";
}
Free download pdf