I have an AJAX POST function. I want to obtain a successful callback then execute a function. I chose to achieve this with $.when as follows
var url = '/echo/html/';
var json_text = ' ';
var FireOrderCounter = 0;
$.when(
$.ajax({
type: 'POST',
url: url,
data: json_text,
success: function () {
FireOrderCounter++;
alert('successfully completed Action ' + FireOrderCounter);
var millisecondsToWait = 5000;
setTimeout(function() {
FireOrderCounter++;
alert('Done Spinnin ' + FireOrderCounter);
}, millisecondsToWait);
},
dataType: 'html'
})
).then(openWindow());
function openWindow() {
FireOrderCounter++;
alert('opened window' + FireOrderCounter );
}
success callback fires after openWindow(). Does this mean that $.ajax is somehow not deferred and $.when is simply assuming success as described in the API?
If a single argument is passed to jQuery.when and it is not a
Deferred, it will be treated as a resolved
This is a simplified test case. The production code fails similarly. I have to fire this event twice to obtain all the data. There is a race condition.
I can insert a breakpoint to make the processing stop. The Function fires while I am still holding onto the debugger. So, its not waiting for a success callback. A short timeout (occurs after ~10 seconds)? How can I fix this?
I think it’s your then statement:
.then(function(){openWindow()});or.then(openWindow). If you pass with () it will execute the function.