I have a function that I call from a from a timeout that makes an ajax call. I also have a button click call that I don't want to execute until the ajax call completes, if the ajax call is running. If the ajax call is not running, I want to run the button click code.
What is the best way to handle this? With a variable that you check to see if the ajax function is running or is there another way?
Here is my pseudo code
setTimeout(doAjax, 5000);
function doAjax() {
$.ajax({
url: "xxx.com/dosomething",
dataType: 'json',
type: 'POST',
cache: false,
contentType: 'application/json; charset=utf-8',
success: function (data) { doSuccess() },
error: function (data, status, jqXHR) { doError() }
});
}
function myBtnClick() {
// wait for doAjax call to complete if running
}