Vimeo app for tracking progress

Hi everyone,

I have the following function as an interval to check a vimeo video’s progress every 3 seconds. The function is suppose to stop the interval when I hit the pause button…
but clearInterval does not work.

 var int1 = setInterval(function() {
        player.getCurrentTime().then(function(seconds) {
            player.getPaused().then(function(paused) {
                if (paused == 'true'){
                    clearInterval(int1);  
                    int1 = 0;
                }
            });
            console.log(seconds);
                        
        });
    }, 3000);

Hi @henk3, the paused value is a boolean not a string, so you have to check

player.getPaused().then(function (paused) {
  if (paused === true) {
    clearInterval(int1)
  }
})

or just

player.getPaused().then(function (paused) {
  if (paused) {
    clearInterval(int1)
  }
})

However, this way the interval will get cleared right away as the player is initially in a paused state; what you have to do instead to make it work is listening to start / pause events:

var handle

var startPolling = function () {
  handle = window.setInterval(function () {
    player.getCurrentTime().then(console.log)
  }, 3000)
}

var stopPolling = function () {
  window.clearInterval(handle)
}

player.on('play', startPolling)
player.on('pause', stopPolling)

Or actually listen to the timeupdate event directly rather then manually polling the player state:

player.on('timeupdate', function (event) {
  console.log(event.seconds)
})
1 Like

Thank you! That made it worked :+1::wink:

np ;-)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.