1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Introduction to Ajax

12. Aborting Ajax

clr gif

Because AJAX is normally processed asynchronously we may have situations where we want to stop waiting for the responce to come back from the server because it is no longer relevant. The situation where your visitor leaves the page before the response is received is handled by the automatic deletion of the request object along with the rest of the page so the situation where we are most likely to need to abort a prior request is when our visitor wants to make a new request (for example they have changed their mind about which option they wanted).

A minor alteration to the code where we create a new ajax object can readily allow our object to be reused after aborting any outstanding request. Here is the code that we need to replace:

var ajaxObj = createXMLHttp();

By replacing this statement with an if statement to test if the object already exists and to abort any exiting request we can reuse an existing object where one already exists and avoid problems with multiple requests to the server.

if (!ajaxObj)
ajaxObj = createXMLHttp();
else if (ajaxObj.readyState != 0)

After running this piece of code if doesn't matter whether a prior request existed as the state of the AJAX object after running this code is the same regardless of whether the object previously existed or has been newly created.

See More About

©2017 About.com. All rights reserved.