It turns out that I have a function that uploads an image to the server, but sometimes it takes too long and I would like to be able to cancel said function using a cancel button.
$('#imageUploadForm').on('change',(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type:'POST',
url: $(this).attr('action'),
data:formData,
cache:false,
contentType: false,
processData: false,
success:function(data){
console.log("success");
console.log(data);
},
error: function(data){
console.log("error");
console.log(data);
}
});
}));
How could the user stop it with a click?
According to the documentation you can use the XMLHttpRequest.abort() method to abort the request.
But once sent, you can't really stop the request, that is, what you do is stop on the client side but the process on the server continues.
You should either send a separate request to the server, so that it interrupts what it is doing, or it would be better to simply ignore the response.
I leave you the code to abort the Ajax request and after 3 seconds the buttons are activated again for a new submission.