简体   繁体   English

如何在while循环中只显示一次文本?

[英]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. 我试图显示一个javascript对象的“text”属性,以便在达到其timecode属性时显示在控制台中。 The "timecode" property is being compared to elapsed time in a Vimeo player. 将“时间码”属性与Vimeo播放器中的已用时间进行比较。 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. 这很好,我遇到的问题是由于Vimeo API每秒返回多个毫秒数据“命中”,所以我看到我的文本在控制台中多次弹出。

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... 有点不完整,但你得到的原则......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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