2

I have:

function send(){
  $.get("/site/send.php", function(data){
    alert(data);
  }
}

In site/send.php I have:

sleep(1000)
echo "OK";

Next I have in my js file:

send();
$("#click").click(function(){
  $.get("/site/reset.php", function(data){
    alert(data);
  }
})

and in reset.php:

echo "RESET";

In this example I have XHR until sleep < 1000. In Firebug I see this. I don't have response and succes in function send() until sleep is < 1000. this is ok, but I would like cancel this request if I use .click().

Now I have all time function send() and until she does not stop (> 1000) then I don't have start with function with .click(). Why this is not async?

Is this possible? Maybe it is possible to cancel all request on the site?

Jason
  • 15,017
  • 23
  • 85
  • 116
Carl Nogry
  • 65
  • 7
  • 1
    You could have an intermediate PHP script which manages the tasks. So, you call the AJAX function which in turn calls the Manager.php script. The Manager.php script then runs more Worker1.php,Worker2.php,..WorkerX.php scripts (and doesn't wait for them to finish, just launches them using CLI) which do the work and update the work status in a database table. After this you can use an AJAX poll to check on the status once and awhile and you can mark a worker to sop executing in the status table (which in turn you check in the worker). It's a bit of work but hope it helps :) – Catalin Dec 12 '11 at 10:36
  • thanks, please give me simply example :) – Carl Nogry Dec 12 '11 at 11:25

2 Answers2

5

Every request you make is actually a separate instance or thread. You can't cancel one request by issuing another request. What you can do is cancel the current request(s) on the client side. Read this question.

Community
  • 1
  • 1
bummzack
  • 5,805
  • 1
  • 26
  • 45
5

You'll have to keep a reference to your XHR object, in order to abort it.

Something like this:

var myXhr = null;
$("#click").click(function(){
  myXhr = $.get("/site/reset.php", function(data){
    alert(data);
  });
})

And then to abort:

if (myXhr) {
  myXhr.abort();
}

Although I'm really unsure about the syntax.

You might want to look at some similiar questions.

Community
  • 1
  • 1
Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106