Reputation: 2309
I have a function that is called by a dropdown change. It is possible that the function is running and the user changes the dropdown.
If that happens I want the function that is running to stop.
Ex.
$('.txtCategory').change(function(){
ajaxPost('para', 'meters').stop(); //doesn't work
$('#Submit').attr('data-check', 'on');
$('#Submit').val('Stop Checking');
});
Is this possible?
Thanks in advance!
Greets, Guido
Upvotes: 1
Views: 2911
Reputation: 145478
As others stated it is impossible to stop a function out of another function in JavaScript.
However, you may use flags to check whether the Ajax request is now running. For that purposes jQuery has non documented $.active
property. You may use it as follows:
$(".txtCategory").change(function() {
if ($.active > 0) // if there are more than 0 Ajax requests running
return false; // at a time we stop executing the current callback
ajaxPost("para", "meters");
$("#Submit").data("check", "on").val("Stop Checking");
});
Upvotes: 0
Reputation: 3061
As others mentioned It is not possible to stop/abort a function from another as they both are sync process. On the other hand if you want to stop/abort a AJAX (first A stands for Asynchronous) request you can do that with the help of jQuery's AJAX return object. To do that you can follow the following steps;
First create a global variable to handle AJAX object;
var xhr = null;
When you request by AJAX assign the return object to this variable;
xhr = $.ajax(
url: "http://someurl.org",
success: function() { ... }
);
Now when you need to abort request just a xhr.abort()
call should do the trick.
if (xhr != null)
xhr.abort();
Hope that helps;
Upvotes: 0
Reputation: 471
@Guido
I think you want some other things.. Please explain your question in better... I think this isn't what you really want achieve..
Also you can use these
Create a setTimeOut function call and run it every 1000ms so whenever you want to
stop that setTimeOut function you can simple use clearTimeOut to stop it.
it will help you out. if you have still any issues let me know :)
Upvotes: 0
Reputation: 382474
This isn't possible for a simple reason : your script execution happens on only one thread, only one function can run at a time.
Any callback responding to a local event or to a query won't be called until the current function has finished execution.
Upvotes: 2