Background: When user clicks a button its class is toggled between class1 and class2 and this data is submitted via AJAX. To confirm this data is saved, server responds with js (updates button HTML).
The Problem: if a user hits the button faster than the server can respond (ie hits the button quickly), the button will toggle back and forth once the server responses actually arrive:
- User hits button, changes to
class2via js (AJAX submitted) - User hits button, changes to
class1via js (AJAX submitted) - Server responds to first AJAX request, and refreshes html to
class2 - Server responds to second AJAX request, and refreshes html to
class1
How could the ajax request be cancelled, if a new one is made?
$('.button').click(function() {
$('this').toggleClass('class1 class2')
// submit ajax request
$.ajax({
url: '/update_link'
})
});