I've been doing a lot of form submission using Ajax now and being used to it than having a normal submission. What's been troubling me is that I always ran with a double submission issue when the user clicks twice the submit button to make sure hey hit it.
One way I handle it is by using a flag:
ajaxIsLoading = false; // initiate flag
function listItem() {
// check if ajax is not loading anymore
if(!ajaxIsLoading) {
ajaxIsLoading = true;
$.post( "/post")
.done(function( data ) {
// data successfully submitted
// set flag to false since post is done
ajaxLoading = false;
});
}
}
One problem I know using this is when your internet connection stops in the middle of it and didn't make through setting the flag
back to false
.
What I want to ask:
- Is this a good way to handle this kind of circumstance?
- Is it worth to check your network in case it went down so I can set the
flag
back tofalse
knowing there's a very little chance it will happen?