简体   繁体   English

作为参数传递给其他函数的函数正被乘法触发

[英]Function passed as parameter in other function is being fired multiplicatively

I have a function that creates a modal window with a "Confirm" and "Cancel" button. 我有一个函数,可使用“确认”和“取消”按钮创建模式窗口。 The function requires an action parameter, through which another function is called. 该函数需要一个action参数,通过该参数可以调用另一个函数。 In the example, this action is the unsubscribe AJAX call. 在示例中,此action是取消订阅AJAX调用。

Only after "Confirm" is clicked, the AJAX call is made. 仅在单击“确认”后,才进行AJAX调用。

Here's when weird things happen. 这是奇怪的事情发生的时候。 After the first time, each and every time I click the "Confirm" button again, it fires the action another time. 第一次之后,每次我再次单击“确认”按钮时,都会再次触发该操作。 So the first time it fires the action just once, the second time it fires it twice, the third time it fires it thrice. 因此,它第一次触发动作一次,第二次触发动作两次,第三次触发动作三次。 Etc. 等等。

I've been scratching my head over this for a while. 我已经为此挠了一下头。 It almost seems as if the action parameter keeps it's functionality, and every newly inputted action is added on top of that. 几乎好像action参数保持其功能,并且每个新输入的action被添加在此之上。

$.createActionModal = function( params, action ) {      
    if ( typeof params.title === 'undefined' ) { params.title= ''; }

    var actionModal = $( '#action-modal' );

    actionModal.find( '.modal-title' ).html( params.title );

    actionModal.show();

    actionModal.on( 'click', '.confirm-button', function( e ) {
        e.preventDefault();

        // Fire the action if "Confirm" is clicked
        action();

        actionModal.hide();

        return true;
    } );

    actionModal.on( 'click', '.cancel-button', function( e ) {
        e.preventDefault(); 

        actionModal.hide();

        return false;
    } );
}

Then in another file, I use this function to show a modal window with two options, Confirm and Cancel. 然后在另一个文件中,我使用此功能显示一个模态窗口,其中包含两个选项:“确认”和“取消”。

$(document).on( 'click', '.unsubscribe-button', function( e ) {
    e.preventDefault();

    var thisButton = $(this);

    var data = {
        'action': 'unsubscribe',
        'author_id': parseInt( thisButton.attr( 'data-author-id' ) ),
    };

    // Modal function is called here, the action paramater is a function wrapping the AJAX function
    $.createActionModal(
        {
            'title':    'Are you sure you want to unsubscribe?'
        },
        function() {
            subscription_ajax( thisButton, data );
        }
    );

});

And finally, the AJAX call that's being passed as the action . 最后,作为action传递的AJAX调用。

var subscription_ajax = function( thisButton, data ) {
    $.ajax({
        url: ajax_url,
        type: 'POST',
        data: data,
        success: function( response ) {     

            console.log( 'Testing' );

            // ... Does ajax stuff

        }
    });
}

Looks like with every click on $('.unsubscribe-button'), $.createActionModal() is called again, attaching new event listeners to its buttons (while the old ones are still there). 看起来像每次单击$('。unsubscribe-button')时,都会再次调用$ .createActionModal(),将新的事件侦听器附加到其按钮上(而旧的事件侦听器仍在那里)。

Create modal and attach listeners once, and on button click only show it. 创建模式监听器并附加一次监听器,然后在按钮上单击仅显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM