admin管理员组文章数量:1402975
I have following method which execute a video clip and on progress of the video clip it does one alert, after that alert it was suppose to close the listener but it is not ending the event listener as a result the alert keep showing forever like infinite.
Is this a BUG? or there is something missing in my following code?
function video_present(input) {
$('#mediaplayer').prop('loop', false);
$('#mediaplayer').attr('src', filename).show();
mediaplay_video= document.getElementById('mediaplayer');
mediaplay_video.play();
// STOP repeating??
mediaplay_video.addEventListener('timeupdate', function() {
var sss = parseInt(mediaplay_video.currentTime % 60);
show_second();
}, false);
}
// kill Event after 1 time execute of this
function show_second() {
alert('I was executed - stop me now if you can??');
mediaplay_video.removeEventListener('timeupdate', function() {
alert('I am killed, but why am i again getting called???');
});
}
video_present('Terminator_10.webm');
I have following method which execute a video clip and on progress of the video clip it does one alert, after that alert it was suppose to close the listener but it is not ending the event listener as a result the alert keep showing forever like infinite.
Is this a BUG? or there is something missing in my following code?
function video_present(input) {
$('#mediaplayer').prop('loop', false);
$('#mediaplayer').attr('src', filename).show();
mediaplay_video= document.getElementById('mediaplayer');
mediaplay_video.play();
// STOP repeating??
mediaplay_video.addEventListener('timeupdate', function() {
var sss = parseInt(mediaplay_video.currentTime % 60);
show_second();
}, false);
}
// kill Event after 1 time execute of this
function show_second() {
alert('I was executed - stop me now if you can??');
mediaplay_video.removeEventListener('timeupdate', function() {
alert('I am killed, but why am i again getting called???');
});
}
video_present('Terminator_10.webm');
Share
Improve this question
edited Jun 24, 2021 at 23:02
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Aug 22, 2016 at 6:34
user285594user285594
3
- 1 You can't kill anonymous event listeners. Define a named handler function, and use a reference to that function when adding/removing listeners. – Teemu Commented Aug 22, 2016 at 6:35
-
@Teemu, i did that but still same. i already did like this which also failed.
addEventListener('', not_annoymouse_lister() );
– user285594 Commented Aug 22, 2016 at 6:40 -
1
addEventListener('', not_annoymouse_lister);
- without the prantheses, or else you'll be setting what the function returns as the event handler, instead of the function itself. – techfoobar Commented Aug 22, 2016 at 6:41
1 Answer
Reset to default 3The second argument to removeEventListener
is the listener function itself. If you do not pass the same argument as with addEventListener
, it will not be removed. Use a named function or a function variable to ensure the same function object is used in both places:
function handleTimeUpdate() {
var sss = parseInt(mediaplay_video.currentTime % 60);
show_second();
}
mediaplay_video.addEventListener('timeupdate', handleTimeUpdate, false);
...
mediaplay_video.removeEventListener('timeupdate', handleTimeUpdate);
本文标签:
版权声明:本文标题:javascript - How to stop eventlistener, which is even keep triggering after killing it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744091833a2589565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论