I have some code, posting data from “my_form” to server with JQuery $post every 3 seconds.
Everything works fine, but sometimes maybe because of slow Internet connection the response doesn’t come in 3 seconds, when cycle triggers.
This makes an error. Of course, I can increase the delay from 3 seconds to 6 and more, but it slows down the process.
How can I change the code to post data only after getting the response?
$(document).ready(function() {
$("#my_form").submit(function() {
document.forms["my_form"]["submit"].disabled="true";
counter=0;
var post_data = function() {
$.post("formProcessor.php", $("#my_form").serialize(),
function(data) {
$("#formResponse").html(data);
txt = $("#formResponse").text();
...
//some code analyzing response
...
if (counter == 50){
clearInterval(nre);
return false;
};
changeFields(counter);
}
);
};
var nre=setInterval(post_data,3000);
return false;
});
});
I would do something like in code below. Pretty simple. Just additional variable
requestFinishedwhich is set to true if request is done .