如何在单击按钮时删除事件监听器?

时间:2019-12-20 10:11:56

标签: javascript javascript-events

我想在单击按钮时删除两个事件侦听器。

这些是我要删除的事件监听器:

this.succesStatus = 'succes-status';
this.notSucces = 'not-succes';

addListenerEvents() {
    window.addEventListener(this.succesStatus, event => {
        try {
            this.statusVal.values[0] = this.succesStatus;
            this.phoneField.values[0] = event.detail.phoneNumber;
            this.parent.refresh(true);
        } catch (e) {
            return false;
        }
    });
    window.addEventListener(this.notSucces, event => {
        try {
            const tstObjectArray = [
                {
                    name: 'statusInformation',
                    value: 'status'
                },
                {
                    name: 'statusKey',
                    value: 'status101 , ' + event.detail.statusKey
                }];
            this.AnalyticsService.runEvent(tstObjectArray);
            this.statusVal.values[0] = this.notSucces;
        } catch (e) {
            return false;
        }
    });
}

试图将函数复制到新函数,并将addEventListener更改为removeEventListener,但这没用。

这是我尝试过的:

removeListenerEvents() {
    window.removeEventListener(this.succesStatus, event => {
        try {
            this.statusVal.values[0] = this.succesStatus;
            this.phoneField.values[0] = event.detail.phoneNumber;
            this.parent.refresh(true);
        } catch (e) {
            return false;
        }
    });
    window.removeEventListener(this.notSucces, event => {
        try {
            const tstObjectArray = [
                {
                    name: 'statusInformation',
                    value: 'status'
                },
                {
                    name: 'statusKey',
                    value: 'status101 , ' + event.detail.statusKey
                }];
            this.AnalyticsService.runEvent(tstObjectArray);
            this.statusVal.values[0] = this.notSucces;
        } catch (e) {
            return false;
        }
    });
}

0 个答案:

没有答案