如何在if循环中只执行一次

How to execute something only once within if-loop?

本文关键字:一次 执行 if 循环      更新时间:2023-09-26

EDIT(更具体):

我有以下情况。我正在用JavaScript构建一个实验性的合成器接口。我正在使用howler.js插件来处理音频。这个想法是,如果满足某个条件,就会播放一个声音。我有一个数字存储变量,其值通过iPhone指南针的标题更改我使用compass.js。它有一个名为heading的变量

$(function(){
    Compass.watch(function (heading) {
      $('.degrees').text(Math.floor(heading) + '°');
      $('#rotate').css('-webkit-transform', 'rotate(' + (-heading) + 'deg)');
       if (heading >= 10 && heading <= 30) {
       sound.play("myFirstSound"); 
       } 
       else if (heading >= 31 && heading <= 60) {
       sound.play("mySecondSound");
       } else { etc etc...
       });
});

即使航向不变,.play命令也不会执行一次,而是一次又一次地执行,导致音频引擎内存不足。如何在if循环中只执行一次命令?我无法访问指南针功能之外的航向值。我总是得到var标题不存在错误。

取一个类似的布尔变量

var checkOnce= false;
if (myVariable >= 10 && myVariable <= 30) {
checkOnce=true;
    sound.play("myFirstSound"); 
    } 
    else if (myVariable >= 31 && myVariable <= 60) {
checkOnce=true;
    sound.play("mySecondSound");
    } else { etc etc...

在循环中检查checkOnce的值。循环将运行到checkOnce=false

创建一个boolean flag并仅在false时播放声音。

var isPlayed = false;
var oldValue = myVariable;
if (myVariable >= 10 && myVariable <= 30 && !isPlayed) {
    sound.play("myFirstSound"); 
    isPlayed = false;
} 

然后在需要时初始化变量。。。对于你所说的,我想你需要一个变量来比较循环中的值,并将isPlayed标志设置为false:

if (myVariable != oldValue)
    isPlayed = false;

只需使用一个布尔变量。

var alreadyPlayed = false;
for(somecondition){
   if(alreadyPlayed) continue;
   sound.play("myFirstSound");
   alreadyPlayed = true;
}

你明白了。

布尔值是个好主意。您也可以使用sound对象的onend属性,这样您就可以知道何时可以再次开始播放声音。像这样:

var playing = false;
sound.onend = function () { playing = false;  };
while (condition) {
   if (!playing & ...)
       ...
   ...
}

看到你对问题的编辑,给下一个触发器添加延迟,指南针可能发射得太快了。不熟悉compass.js,但正如我所看到的,它每度发射一次,这是很多。。。如果你把手机倾斜180度,就会触发180度。类似

setTimeout(function() { your_func(); }, 50); 

对于延迟50ms的

您还可以通过添加一个10的计数器将可用的触发器从360限制为36或更少,1度表示1次计数。所以它只有在10度运动后才会触发。

$(function(){
var counter = 0;
Compass.watch(function (heading) {
   if(counter <=10){
     counter++;
    }
    else{ 
   counter = 0;
  $('.degrees').text(Math.floor(heading) + '°');
  $('#rotate').css('-webkit-transform', 'rotate(' + (-heading) + 'deg)');
   if (heading >= 10 && heading <= 30) {
   sound.play("myFirstSound"); 
   } 
   else if (heading >= 31 && heading <= 60) {
   sound.play("mySecondSound");
   } else { etc etc...
   }       
   });
});

只添加一个小的抽象层,用于跟踪当前正在播放的内容。

var nameOfSoundPlaying = null;
function play(soundName) {
   if (nameOfSoundPlaying == soundName) { 
      return; 
   } else {
     if (nameOfSoundPlaying != null) {
        // stop currently playing sound (if still playing)
        ...
     }  
     nameOfSoundPlaying = soundName;
     sound.play(soundName);
   }
}

然后它会简化你的代码:

$(function(){
    Compass.watch(function (heading) {
      $('.degrees').text(Math.floor(heading) + '°');
      $('#rotate').css('-webkit-transform', 'rotate(' + (-heading) + 'deg)');
       if (heading >= 10 && heading <= 30) {
           play("myFirstSound"); 
       } 
       else if (heading >= 31 && heading <= 60) {
           play("mySecondSound");
       } else { etc etc...
       });
});