AJAX beauty – AJAX Request ABORTing – How To

AJAX (Asynchronous Javascript And Xml) – most of us know how better we can use this and how much it can help us in our project. To give you a brief intro on AJAX – getting many things (information, data, etc) in an ASYNCHRONOUS (easy) way meaning “without refreshing the page” – I suggest you read this article (http://en.wikipedia.org/wiki/AJAX) to understand AJAX in detail. I recommend this article for people who understands AJAX.

For Experts:

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.

For Beginners:

Ok, what is this POST all about? I said “ajax.abort” – we know using AJAX we can get many information, data, etc….asynchronously by creating multiple javascript-calls and each calls will be responsible to get an information sent from the server – see I said multiple calls that means we can’t rely on response-time. Information you get from these calls can be mixed, we can’t guarantee that we will get the response on time for every request it depends on various things – some might come faster some might come slower. Having said that, either you might be confused to associate the data to their calls or the data is no longer relevant.

How to Tackle:

How do you tackle this? Simple, with the help of XMLHttp() object.

// creating the xmlhttp (AJAX) request blindly
var ajaxObj = createXMLHttp(); // for jQuery, it is $.ajax…
// creating the xmlhttp (AJAX) request only when the line
// is clear (no pre-xmlhttp calls)
if (!ajaxObj) {
ajaxObj = createXMLHttp(); // for jQuery, it is $.ajax…
// XMLHttp.readyState with not equal 0 tell you if any
// call is still waiting for it’s response.
else if (ajaxObj.readyState != 0) {
// abort the existing calls if any

I hope the above example is self-explanatory. Let me know if you need a better detailed explanation.

Happy Coding!

AJAX beauty – AJAX Request ABORTing – How To

One thought on “AJAX beauty – AJAX Request ABORTing – How To

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s