javascript - How to stop eventlistener, which is even keep triggering after killing it? - Stack Overflow

I have following method which execute a video clip and on progress of the video clip it does one alert,

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
Add a ment  | 

1 Answer 1

Reset to default 3

The 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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744091833a4557222.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信