Skip to content

Instantly share code, notes, and snippets.

@amitmerchant1990
Last active August 29, 2015 13:56
Show Gist options
  • Save amitmerchant1990/8915141 to your computer and use it in GitHub Desktop.
Save amitmerchant1990/8915141 to your computer and use it in GitHub Desktop.
JavaScript snippet for aborting ajax rquest if previous call has not been completed
jQuery(document).ready(function(){
    var currentRequest = null;
    jQuery('#searchbox').keyup(function() {
        var text = jQuery(this).val();
        currentRequest = jQuery.ajax({
            type: 'POST',
            data: 'search_text=' + text,
            url: 'AJAX_URL',
            beforeSend : function()    {           
                if(currentRequest != null) {
                    currentRequest.abort();
                }
            },
            success: function(data) {
                jQuery('#data').html(data).show();
            }
        });
    });});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment