1

What I want:

  1. Monitor a player to execute a function when it reach 85% of the movie - Ok
  2. Execute a PHP script that insert some data into a Mysql table - Ok
  3. Do this only one time (stop looping), since I want only one row in the Mysql table - Fail

My code:

jwplayer().onTime(function(evt) {
    if (evt.position > ([evt.duration] * (85/100)) && x!=1) {
        loadXMLDoc();
        var x = 1;
    }
});

Thanks

1
  • Do you want the listener to run just once with every page load, or every time the video starts and plays through? Commented Aug 29, 2011 at 22:30

2 Answers 2

2

The problem is that x gets reset everytime

jwplayer().onTime(
    (function () {
        var check=true;
        return function(evt) {
            if (check && evt.position > ([evt.duration] * (85/100))) {
                loadXMLDoc();
                check=false;
            }
        }
    })()
 );
Sign up to request clarification or add additional context in comments.

Comments

0

If you want the function to run only once with each page load, another approach is to make a function that commits suicide.

(function (player) {
    var checkAndLoad = function(evt) {
        if (evt.position > (evt.duration * (85/100))) {
           loadXMLDoc();
           checkAndLoad=function(evt){};
        }
    };
    player.onTime(function(evt) {checkAndLoad(evt);});
})(jwplayer());

You need the extra indirection provided by the anonymous wrapper since onTime gets its own copy of the event listener, so overwriting checkAndLoad won't affect the registered listener directly.

If you want the listener to run more than once, register additional listeners that restore checkAndLoad at the appropriate events (e.g. the user seeks back to near the beginning).

(function (player) {
    var timeListener;
    function checkAndLoad(evt) {
        if (evt.position > (evt.duration * (85/100))) {
           loadXMLDoc();
           timeListener=function(evt){};
        }
    }

    timeListener = checkAndLoad;

    player.onTime(function(evt) {timeListener(evt);});

    player.onSeek(function(evt) {
        if (evt.position < (evt.duration * (15/100))) {
           timeListener=checkAndLoad;
        }            
    });
    player.onComplete(function (evt) {timeListener=checkAndLoad;});
})(jwplayer());

Better would be to unregister the listener, but the JW Player API doesn't currently expose the removeEventListener method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.