need help on ajax when the same function with call ajax is called continuously

advertisements

I need assistance on ajax because I am new to it. When same ajax call is called multiple times before that ajax call completes execution its throwing empty pop up on ui. How can I solve this problem. Actually when user clicks on download link that code is written in success function of ajax. But if user clicks the download link multiple times before downloading the first one. Its throwing error. How can i solve this?

this is my code

function clickLink(section) {

    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: 'text',
        url: 'MyService.asmx/getMetrics',
        data: { programName: progName, startYear: beginYear, endYear: endYear, userName: spnUserName.innerHTML, rdate:dt },
        success: function (data) {
            var jdata = data;
            jdata = jdata.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
            jdata = jdata.replace('<?xml version="1.0" encoding="utf-8"?>', '');
            jdata = jdata.replace('\r\n', '');
            var link = document.createElement("a");
            link.setAttribute('href', jdata.trim());
            link.onclick = OpenFile(jdata.trim());
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}


You can set a temporary class.

$('.your-element:not(.disabled)').click(function(){
    var $this = $(this);
    $this.addClass('disabled');

    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: 'text',
        url: 'MyService.asmx/getMetrics',
        data: { programName: progName, startYear: beginYear, endYear: endYear, userName: spnUserName.innerHTML, rdate:dt },
        success: function (data) {
            var jdata = data;
            jdata = jdata.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
            jdata = jdata.replace('<?xml version="1.0" encoding="utf-8"?>', '');
            jdata = jdata.replace('\r\n', '');
            var link = document.createElement("a");
            link.setAttribute('href', jdata.trim());
            link.onclick = OpenFile(jdata.trim());
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    }).done(function(){
       $this.removeClass('disabled');
    });

    return false;
});