superninja
superninja

Reputation: 3411

Sequential Events on JavaScript for on Function

For video player, there are 3 actions: play, pause and continue. For continue action, it is defined as play after being pause.

If I have 2 functions already defined for play and pause:

videoPlayer.on('play', function () {
      //play video
});
videoPlayer.on('pause', function () {
      //pause video
});

I am having trouble defining the sequential action of pause -> play. Would the below function work for continue?

videoPlayer.on('pause').on('play', function () {
       //Continue video           
})

Upvotes: 0

Views: 65

Answers (1)

AnanthDev
AnanthDev

Reputation: 1818

If I understood your question correctly, you could use promises to wait till the video is paused

function playP() {
    videoPlayer.on('play', function () {
        //play video
    });
}

function pauseP() {
    return new Promise((res, rej) => {
        videoPlayer.on('pause', function () {
            // do stuff
            res(true);
        });
    })
}

pauseP().then((x) => {
    if (x) 
        playP();
})

This code will wait till the video is paused to start listening for the play event.

Upvotes: 2

Related Questions