简体   繁体   中英

How do I display text once and only once in a while loop?

I am attempting to display a javascript object's "text" property to display in the console when its timecode property is reached. The "timecode" property is being compared to elapsed time in a Vimeo player. That's fine and well—the issue I am having is due to the Vimeo API returning multiple millisecond data 'hits' per second, so I see my text pop up multiple times in the console.

Can anyone suggest how to display each text property once, and only once?

notes_ex = [
  {
    timecode: 2,
    text: 'Hi there!'
  },
  {
    timecode: 7,
    text: 'Hi again!'
  }
];

function ready(player_id) {
  var player = $f(player_id);

  player.addEvent('ready', function() {
      console.log('ready');
      player.addEvent('playProgress', onPlayProgress);
  });

  function onPlayProgress(data, id) {
    timeElapsed = Math.floor(data.seconds);
    console.log('timeElapsed:' + timeElapsed);

    while (timeElapsed++) {
      for (var i = 0; i < notes_ex.length; i++) {
        timecode = notes_ex[i].timecode;
        if (timecode === timeElapsed) {
          console.log(notes_ex[i].text);
        }
      }
      break;
    }
  } 

Would you just be able to assign a second variable to denote when it's been triggered? So something like this:

var z = 0;

if (timecode === timeElapsed && z == 0) {
  console.log(notes_ex[i].text);
  z++;
}

A bit incomplete, but you get the principle...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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