How to override the jQuery.ajax success function after it has been initialized?

前端 未结 3 1272
不知归路
不知归路 2021-01-21 22:55

A button click triggers an ajax request. When the user clicks the button a second time while the first request is still loading, i want to override the first request\'s success

3条回答
  •  心在旅途
    2021-01-21 22:59

    You can try the following hack, I have tested it with asynch setTimeout (instead of asynch jQuery.ajax) and it works -

    var mySuccessHander = function() {
        console.debug('Initial function');
    }
    
    var test = jQuery.ajax({
        url: '...',
        success: function() {
            mySuccessHander();
        }
    });
    

    And when the button is clicked for the second time, execute following -

    mySuccessHander = function() {
        console.debug('Overridden function');
    }
    

提交回复
热议问题