Evan Emolo
Evan Emolo

Reputation: 1670

Function to listen for global variable change?

Novice javascript question here: I have two functions. The first function returns data from a video player api when the video player is playing. The second function should execute code depending on whether function 1 is retrieving data.

Something like this:

var playing = false;

// Function 1
function onPlayProgress(data, id) {
  if (data) {
    playing = true;
  }
} 

// Function 2
function movePlayhead() {
  if(playing) {
    console.log('playhead moving')
  } else {
    console.log('playhead stopped')
  }
}

movePlayhead();

My problem is function 2 is only called when the file loads, not continuously. How can I do this?

Upvotes: 1

Views: 1285

Answers (1)

dinjas
dinjas

Reputation: 2125

Use setInterval.

setInterval(function() {
  if (playing) { 
      ...
  } else {
      ...
  }
}, 500);

Or (as @bfavaretto pointed out):

setInterval(movePlayhead, 1000);

Upvotes: 3

Related Questions