简体   繁体   English

如果指定了回调函数,则从内部删除事件侦听器

[英]Remove event listener from the inside if specified callback function

I have a situation, where I want to attach a function with parameters to an event listener, like this: 我遇到一种情况,我想将带有参数的函数附加到事件侦听器,如下所示:

var pauseAudioAt = function(aud, seconds, removeListener) {
    console.log(aud.currentTime);
    // check whether we have passed 5 minutes,
    // current time is given in seconds
    if(aud.currentTime >= seconds) {
        // pause the playback
        aud.pause();
        if (removeListener) {
            aud.removeEventListener('timeupdate', pauseAudioAt);
            showBtn();
        }
    }

}
audio.addEventListener("timeupdate", function() {
                pauseAudioAt(audio, 18, true);
            });

I want to remove the listener as soon as the function invoked? 我想在函数调用后立即删除监听器吗? How can I achieve this ? 我该如何实现?

Thanks. 谢谢。

You have to pass .removeEventListener() a reference to the same function that you passed to .addEventListener() . 你必须通过.removeEventListener()到您传递给相同功能的参考.addEventListener() One way to do that with minimal change to your existing code would be to name your (currently anonymous) function expression, then pass that function to pauseAudioAt() instead of passing the boolean: 一种对现有代码进行最小更改的方法是命名您的函数(当前为匿名),然后将该函数传递给pauseAudioAt()而不是传递布尔值:

var pauseAudioAt = function(aud, seconds, listenerToRemove) {
    console.log(aud.currentTime);
    // check whether we have passed 5 minutes,
    // current time is given in seconds
    if(aud.currentTime >= seconds) {
        // pause the playback
        aud.pause();
        if (typeof listenerToRemove === "function") {
            aud.removeEventListener('timeupdate', listenerToRemove);
            showBtn();
        }
    }    
}

audio.addEventListener("timeupdate", function listener1() {
    pauseAudioAt(audio, 18, listener1);
});

That way, pauseAudioAt() doesn't need a hardcoded reference to which function needs to be removed. 这样, pauseAudioAt()不需要硬编码的引用即可删除需要删除的函数。

If you want to call pauseAudioAt() without removing the listener then just omit that argument: pauseAudioAt(audio, 18) - or pass false or null or something if that's more convenient: pauseAudioAt(audio, 18, null) . 如果要在删除侦听器的情况下调用pauseAudioAt() ,则只需忽略该参数: pauseAudioAt(audio, 18) -或传递falsenull或更方便的方法: pauseAudioAt(audio, 18, null)

(If you want to be able to call pauseAudioAt() from some other part of your code and remove the listener then you could combine this with a function declaration as shown in Jaromanda X's answer .) (如果您希望能够从代码的其他部分调用pauseAudioAt() 删除侦听器,则可以将其与函数声明结合使用,如Jaromanda X的答案所示 。)

You can only remove exactly the same function you've added 您只能删除与添加的功能完全相同的功能

in your case you could do 在你的情况下你可以做

// added this
var x = function() {
    pauseAudioAt(audio, 18, true);
}
//
var pauseAudioAt = function(aud, seconds, removeListener) {
    console.log(aud.currentTime);
    // check whether we have passed 5 minutes,
    // current time is given in seconds
    if(aud.currentTime >= seconds) {
        // pause the playback
        aud.pause();
        if (removeListener) {
            aud.removeEventListener('timeupdate', x); // changed this
            showBtn();
        }
    }

}
audio.addEventListener("timeupdate", x); // changed this

Just use named function. 只需使用命名函数即可。

var pauseAudioAt = function(aud, seconds, removeListener) {
    console.log(aud.currentTime);
    // check whether we have passed 5 minutes,
    // current time is given in seconds
    if(aud.currentTime >= seconds) {
        // pause the playback
        aud.pause();
        if (removeListener) {
            aud.removeEventListener('timeupdate', onTimeUpdate);
            showBtn();
        }
    }

}

audio.addEventListener("timeupdate", onTimeUpdate);

function onTimeUpdate() {
    pauseAudioAt(audio, 18, true);
}

It'll remove the handle onTimUpdate for event timeupdate . 它会删除该手柄onTimUpdate事件timeupdate

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

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