开发者

Stop a javascript function

开发者 https://www.devze.com 2023-04-01 03:03 出处:网络
What I want: Monitor a player to execute a function when it reach 85% of the movie - Ok Execute a PHP script that insert some data into a Mysql table - Ok

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();
    开发者_JAVA技巧    var x = 1;
    }
});

Thanks


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;
            }
        }
    })()
 );


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消