0

I am coding a web page that has an embedded jPlayer. The player has a timeupdate attribute that allows you to call a function whenever the time updates (at approximately 4 Hz). I have it calling a function that updates an interactive transcript on the page, that highlights what is currently being said using json data that has timecodes associated with the various parts of the transcript. The function also returns json data to be used the next time it is run, so it can keep track of where it is without having to search the entire json file every time.

Everything works great except one condition that never triggers, where it seemingly should. If the current time is more than 0.75 seconds different than the last time, then I should assume that the user clicked back or ahead and update the transcript accordingly. But I can stop and start again the audio file 10 seconds in, or click far ahead, and the if statement is never triggered. The problem is very simple, but I'm baffled as to what is causing it. What am I missing here? Here is the code:

function updateTranscript(event, transcriptAsJson, divIdRoot, lastStepJson) {
  
  var currentTime = event.jPlayer.status.currentTime;
  
  var currentStepJson = lastStepJson;
  currentStepJson.lastTime = currentTime;
  
  /*
   * If the user navigated the time forward or back
  */
  if(currentTime >= (lastStepJson.lastTime + 0.75) || currentTime <= (lastStepJson.lastTime - 0.75)) {
    //unhighlight lastStepJson.lastHighlightedId and
    //any lastStepJson.additionalHighlightedIds
    alert('user skip');
    //...
  //...
//...

lastStepJson's schema looks like this, by the way.

{
    'lastTime': 0.0,
    'lastUpdate': 0.0,
    'lastHighlightedId': 0
}
Peter Behr
  • 607
  • 1
  • 5
  • 16
  • @Ryan I assumed that that line was debugged code, but it looks like I was wrong. I deleted the comment. – jdmdevdotnet Mar 15 '17 at 01:35
  • 2
    You have objects, by the way, not JSON. JSON is a text representation of objects. – Ry- Mar 15 '17 at 01:39
  • @Ryan Yes, rigorously speaking I'm using a Javascript Object but it was parsed from javascript object notation. I'm sure we're on the same page regardless. – Matthew Jendrasiak Mar 15 '17 at 01:47
  • @Ryan Took me a minute to see that you linked this question to another. I never would have guessed that both objects were a reference to the same, but changing that assignment to use jQuery fixed everything. Appreciate it. – Matthew Jendrasiak Mar 15 '17 at 01:55
  • You also probably don’t need to clone it; just set `lastStepJson.lastTime = currentTime;` at the end of the function. – Ry- Mar 15 '17 at 02:02

0 Answers0