I have some ajax code that works great. Upon button click it calls the doAjax function every 2 secs until it receives a message from the server (success being the text Initializing...please wait).
What I want to add is a timeout in case the server is down or whatever. So I added the timeout and error function:
$(document).on("click", ".play", function() {
function doAjax(){
$.ajax({
url: 'index.php',
success: function(data) {
if (data == 'Initializing...please wait')
{
$('#quote p').html(data).css({'border': '1px solid', 'margin': '10px 0px'});
setTimeout(doAjax, 2000);
}
},
error: function(jqXHR, strError) {
if(strError == 'timeout')
{
$('#quote p').html("Oops! Not found...").css({'border': '1px solid'});
}
},
timeout: 8000
});
}
doAjax();
});
The code still works but I never see a timeout happen after the 8 seconds. I tried removing the recursion but still have the same problem. What am I doing wrong?
Just a wild guess, try first removing the recursion from your ajax call, and see if your timeout is working as required.
Also, why do you have your function definition inside your function call? You should make your function defined outside, the on-click call, and then call it from within.