mardi 31 mars 2015

How to prevent browsers from retrying failed POST requests (and check for internet connectivity)?

I have a site intended for mobile users that would check the connectivity for every POST requests. If there is no internet connectivity, I will prompt our an error message for to user. Also, I would like to have a transition effect indicating the form is being submitted. I have the following codes



var progress = 0;

$('div').on('submit', 'form', function(e){

var thisform = this;

if(progress === 1){

// check internet connection
var connection = hostReachable();

if(!connection){
alert('No connection');
setTimeout(function(){ // re-allow submission after 8000 ms (not immediately to avoid browser retry)
progress = 0;
},8000);
return false;
}

return true;
}

e.preventDefault();
e.stopPropagation();

if(progress === 0){ // first submit

// updated progress value
progress = 1;

//form transition
$('.spinner').show('fast', function(){
$('.pagecontent').fadeOut('fast');
thisform.submit();
});

} else {
// prevent submit retries when no connectivity
return false;
}
});


where hostReachable() is a function for checking connectivity using xhr request.


The problem is now after the thisform.submit(); the form submission event seems not triggered again. So the codes inside if(progress === 1) is never run. Why is it so? What's wrong with my codes? Thanks!


Aucun commentaire:

Enregistrer un commentaire