-3
$(function() {
    function x() {
        $.ajax({
            cache: false,
            type: "post",
            data: 'messages=' + 5,
            url: 'http://url.com',
            complete: function(data) {
                x(true);
            },
            error: function(x, t, m) {
                alert(t);
            },
            dataType: "text"
        });
    }
})

If I use comet approach and I don't get response from url.com and I press on logo, for example, then it gives me alert "error", but I still need to wait until I get response from url.com to be able to go to other page. I guess I should do something in this part instead of alert:

            error: function(x, t, m) {
                alert(t);
            },

Not sure what.

EDIT: I want that person who clicked on the logo or any other link, would be redirected there instantly, not waiting for an ajax response.

good_evening
  • 21,085
  • 65
  • 193
  • 298
  • @Creotiv: Well, I thought it's obvious, sorry. When user presses on logo or on other link, he would be redirected to that URL instantly, not needing to wait for ajax response. So, I should probably just stop ajax somehow. – good_evening Dec 26 '11 at 21:52
  • 3
    I don't follow. Please rewrite your question using these headlines: 1. What I've got right now, 2. How it works, 3. How I would like it to work – Simeon Dec 26 '11 at 21:56
  • 1
    @hey Do you want to disable page unloading (by clicking on any link or refreshing) till Ajax response isn't received? – Pradeep Dec 26 '11 at 22:06
  • Not all browser can handle unload event. So only one correct way(for all situation except window close) it's replace all urls with JS function that will be called on link click. Only then you can always handle link click. – Andrey Nikishaev Jan 02 '12 at 14:54

1 Answers1

0
var xhr = $.ajax({

When You need to stop request use xhr.abort();

for example:

$(document).ready(function() {

    $("a").live("click",function() {
        xhr.abort();
        return true;
    });

*// code which calls AJAX request here*

});
Egor Sazanovich
  • 4,979
  • 5
  • 23
  • 37