简体   繁体   中英

Javascript function called by listener won't update global variable

Ok, So I have an event listener on a video player listening for the "timeupdate" event, which happens 5 - 10 times per second, every time the current timecode of a video player is updated. I'm writing a function that allows me to do something for a set amount of time based on the play position of the video. IE - pause the video at frame 320 and unpause it at frame 420.

To save on processing power, I created a global variable called notStarted which basically tells the function whether to start the event (pause the video) or stop the event (unpause the video) so that when notStarted is true we begin the event and when notStarted is false, we end the event.

This is the function. It is successfully making notStarted false after beginning the event, but it's not changing it back to true afterward.

UPDATE: Here is all the relevant code

    var frameRate = 30;

var players=new Array(_V_("video_canvas_1"), _V_("video_canvas_2"));

var canv1 = players[0];
var canv2 = players[1];

var canv1Play = 1; // Autoplay is enabled, so we set the Play state to 1 off the bat.
var canv2Play = 1; // Autoplay is enabled, so we set the Play state to 1 off the bat.
var conchShell = _V_("conch"); // Lord of the Flies, Bitch!

    var token = 0;


function currentFrame(e) {
    return frameRate * e.currentTime();
    console.log('currentTime * frameRate:' + frameRate * e.currentTime());
}

// Accepts player objects as arguments - canv1, canv2
function pause(){
    for (var i = 0, j = arguments.length; i < j; i++){
        arguments[i].pause();
        console.log( arguments[i] + '1 is paused' ); /* TODO: REMOVE CONSOLE LOGS BEFORE DEPLOYMENT */
    }
}

function play(){
    for (var i = 0, j = arguments.length; i < j; i++){
        arguments[i].play();
        console.log( arguments[i] + '1 is playing' );
    }
}
function syncSingleToConch(e) {
    e.currentTime(conchShell.currentTime());
}

function syncToConchShell() {
    async.parallel([
        async.each(players, syncSingleToConch, function(err){ console.log('error with the each function'); }),
        conchShell.currentTime(arguments[0].currentTime()),
        async.each(players, playSingle, function(err){ console.log('error with the each function'); })
    ]);
}
function pauseAtKeyFrame(startStop, player, frameStart, frameEnd) {

    if (startStop === "start") {
        pause(player);
        console.log('pausing player');
    }
    if (startStop === "stop") {
        play(conchShell, canv1, canv2);
        setTimeout(syncToConchShell(canv1), 3500);
        startStop = "start";
        console.log('playing player');
    }
}
var currentEvent = 0;
var notStarted = true;
var frameEvents = [
    {
        "type": pauseShowModal,
        "frameStart": 200,
        "frameEnd": 220,
        "canvas": canv1
    },
    {
        "type": pauseAtKeyFrame,
        "frameStart": 320,
        "frameEnd": 420,
        "canvas": canv1
    },
    {
        "type": showDivAtTime,
        "frameStart": 111,
        "frameEnd": 311,
        "canvas": canv1,
        "div": "frame100"
    }
    ]; 

function keyFrames() {
    if (notStarted === true && frameEvents[currentEvent].frameStart < currentFrame(conchShell) && frameEvents[currentEvent].frameEnd > currentFrame(conchShell)) {
        frameEvents[currentEvent].type("start", frameEvents[currentEvent].canvas, frameEvents[currentEvent].div);
        notStarted = false;
        console.log(notStarted);
    } else if (notStarted === false && frameEvents[currentEvent].frameEnd < currentFrame(conchShell)) {
        console.log(notStarted);
        frameEvents[currentEvent].type("stop", frameEvents[currentEvent].canvas, frameEvents[currentEvent].div);
        currentEvent++;
        notStarted = true;
    }
}

conchShell.addEvent("timeupdate", keyFrames);

As you may know, Javascript uses function scope, meaning that each variable has the scope of the function that it is declared inside of.

The fact that the same function successfully changes the value of notStarted in one case and not the other suggests that one of three things is happening.

1) The conditions inside your if else statements are not evaluating to true and false the way you expect, or

2) An error could be occurring inside the function causing it to halt execution before it sets the variable the way you expect, or

3) Some other code is changing the value of your variable unexpectedly.

If you don't have a debugger you can attach to step thru the code, I would suggest putting in some alert statements so that you can be certain about how your program is executing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM