(I really didn't know how to title this, feel free to suggest an edit)
Consider the following sample html:
<div class="grid">
<div class="item"><span class="icon"></span></div>
<div class="item"><span class="icon"></span></div>
<div class="item"><span class="icon"></span></div>
<div class="item"><span class="icon"></span></div>
<div class="item"><span class="icon"></span></div>
</div>
Attached to each .icon
is some jQuery:
<script type="text/javascript">
jQuery('.icon').click(function(){
runAjaxStuff();
displayNewContent();
})
</script>
In a nutshell, each time a user clicks an .icon
, the corresponding parent .item
will perform an AJAX request and finish by displaying additional .item
content on the page.
If a user, in this scenario, were to click each of the .item
quickly in succession, the AJAX request from a preceeding click is likely to still be waiting for a response. Of course, the new content is also not displayed yet; however, after a handful of seconds and all the requests have had a chance to complete, the user is then barraged with tons of new content being displayed all at once.
What I am after is a method to simply stop execution of any previous click
bindings if an .icon
is clicked so that only the most recent click ultimately completes.
I am aware that I can make my AJAX call syncronous instead, but I am hoping for a better way that will not seize the user's browser. My jQuery is good enough to get by, but this is something a little more in-depth than I'm used to.