最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove a event handler from JWPlayer instance? - Stack Overflow

programmeradmin3浏览0评论

I'm the using JWPlayer. After setup the player I need to add listeners to some events, to give an example I listen to events.JWPLAYER_MEDIA_TIME like so:

jwplayer('video-container').onTime(this.onTimeHandler);

After a while I need to remove this event listener, reading the documentation I couldn't find any solution.

I'm the using JWPlayer. After setup the player I need to add listeners to some events, to give an example I listen to events.JWPLAYER_MEDIA_TIME like so:

jwplayer('video-container').onTime(this.onTimeHandler);

After a while I need to remove this event listener, reading the documentation I couldn't find any solution.

Share Improve this question edited May 2, 2013 at 10:14 a--m asked May 2, 2013 at 9:44 a--ma--m 4,7821 gold badge41 silver badges60 bronze badges 2
  • have you tried jwplayer('video-container').onTime(null); ?? – yogi Commented May 2, 2013 at 9:49
  • hi @yogi, it doesn't work. The jwplayer.onTime() is bindding a function to an event, I can't find a way to remove this bind... – a--m Commented May 2, 2013 at 10:00
Add a ment  | 

2 Answers 2

Reset to default 8

Looking at the code, it doesn't seem possible to remove an event listener: a callback is pushed onto an array when you call onTime (or any of the other methods to setup event handlers), so calling it a second time doesn't overwrite a previous listener but just adds a new listener to the array.

Perhaps an alternative could be to set a flag once your listener doesn't have to perform its task anymore:

onTimeHandler : function() {
  if (! this.handleOnTimeEvents)
    return;
  ...
}

Here is how I handled it. create a pseudo function whose sole purpose is to be a pointer. I was concerned with the onComplete event, so I wrote the code like so below:

function createJWPlayer(surl, stitle, autos, pw, ph) {
    jwplayer("videocontainer").setup({
        file: surl,
        title: stitle,
        width: pw,
        height:  ph,
        autostart: autos,
        stretching: "uniform",
        skin: "/Scripts/JWPlayer/six.xml"
    });
    jwplayer().onComplete(function (e) {
            jwpleteevent(e);
        });
}

function jwpleteevent(e) {
    // method to remain empty, sole purpose is to provide a pointer for the handler
}

Then in the function where I created it, I wrote this:

var mplete = (selobj.HasQ == false) ? InterActNoTimeAutoS : jwpCompleteInterA;
createJWPlayer(selobj.Upath, ti.TestTitle, true, "100%", "100%");
jwpleteevent = mplete;

If I needed to load another video, I would do this

mplete = (selobj.HasQ == false) ? InterActNoTimeAutoS : jwpCompleteInterA;
jwpleteevent = mplete;
loadJwPlayerUrl(selobj.Upath, true);

If anyone sees a problem with this, please tell me, it seems to be working as needed in the development environment

发布评论

评论列表(0)

  1. 暂无评论