简体   繁体   English

侦听器调用的Javascript函数不会更新全局变量

[英]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. 好的,所以我在视频播放器上有一个事件侦听器,用于监听“ timeupdate”事件,每次更新视频播放器的当前时间代码时,该事件每秒发生5-10次。 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. IE-在第320帧暂停视频,然后在第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. 为了节省处理能力,我创建了一个名为notStarted的全局变量,该变量基本上告诉该函数是开始事件(暂停视频)还是停止事件(取消暂停视频),以便当notStarted为true时,我们开始事件,而notStarted是错误的,我们结束活动。

This is the function. 这就是功能。 It is successfully making notStarted false after beginning the event, but it's not changing it back to true afterward. 在开始事件后,它成功地将notStarted设置为false,但是此后并没有将其更改为true。

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. 您可能知道,Javascript使用函数作用域,这意味着每个变量都具有在其内部声明的函数的作用域。

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. 相同的函数在一种情况下成功更改notStarted的值,而在另一种情况下成功更改notStarted的值,这一事实表明发生了三件事之一。

1) The conditions inside your if else statements are not evaluating to true and false the way you expect, or 1)if else语句中的条件未按您期望的方式评估为真和假,或者

2) An error could be occurring inside the function causing it to halt execution before it sets the variable the way you expect, or 2)函数内部可能发生错误,导致它在按照您期望的方式设置变量之前停止执行。

3) Some other code is changing the value of your variable unexpectedly. 3)其他一些代码意外更改了变量的值。

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. 如果没有调试器,则可以通过代码附加步骤,我建议您添加一些alert语句,以便可以确定程序的执行方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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