簡體   English   中英

如何在while循環中只顯示一次文本?

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

我試圖顯示一個javascript對象的“text”屬性,以便在達到其timecode屬性時顯示在控制台中。 將“時間碼”屬性與Vimeo播放器中的已用時間進行比較。 這很好,我遇到的問題是由於Vimeo API每秒返回多個毫秒數據“命中”,所以我看到我的文本在控制台中多次彈出。

任何人都可以建議如何顯示每個文本屬性一次,只有一次?

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;
    }
  } 

您是否能夠分配第二個變量來表示它何時被觸發? 所以像這樣:

var z = 0;

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

有點不完整,但你得到的原則......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM