Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在if循环中只执行一次?_Javascript_Jquery_Html_If Statement_Web - Fatal编程技术网

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

Javascript 如何在if循环中只执行一次?,javascript,jquery,html,if-statement,web,Javascript,Jquery,Html,If Statement,Web,编辑(更具体): 我有以下情况。我正在用JavaScript构建一个实验性的合成器接口。我正在使用插件来处理音频。 这个想法是,如果满足某个条件,就会播放声音。我有一个数字存储变量,其值通过iPhone指南针的标题改变。 我使用。它有一个名为heading的变量。 $(function(){ Compass.watch(function (heading) { $('.degrees').text(Math.floor(heading) + '°'); $('#

编辑(更具体):

我有以下情况。我正在用JavaScript构建一个实验性的合成器接口。我正在使用插件来处理音频。 这个想法是,如果满足某个条件,就会播放声音。我有一个数字存储变量,其值通过iPhone指南针的标题改变。 我使用。它有一个名为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...
       });
});
$(函数(){
罗盘.手表(功能(航向){
$('.degrees').text(数学楼层(标题)+'''°);
$('#rotate').css('-webkit transform','rotate('+(-heading)+'deg');

如果(heading>=10&&heading=31&&heading创建一个
布尔标志
,仅当
false
时播放声音

var isPlayed = false;
var oldValue = myVariable;

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

只需使用一个布尔变量

var alreadyPlayed = false;

for(somecondition){
   if(alreadyPlayed) continue;

   sound.play("myFirstSound");
   alreadyPlayed = true;
}

你明白了。

取一个布尔变量,如

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...
var checkOnce=false;

如果(myVariable>=10&&myVariable=31&&myVariable布尔值是个好主意。您还可以使用
sound
对象的
onend
属性,这样您就可以知道何时可以再次开始播放声音。如下所示:

var playing = false;
sound.onend = function () { playing = false;  };

while (condition) {
   if (!playing & ...)
       ...
   ...
}

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

setTimeout(function() { your_func(); }, 50); 
延迟50毫秒

您还可以通过添加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计数器=0;
罗盘.手表(功能(航向){

如果(counter=10&&heading=31&&heading只添加一小层抽象,以跟踪当前播放的内容

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...
       });
});
$(函数(){
罗盘.手表(功能(航向){
$('.degrees').text(数学楼层(标题)+'''°);
$('#rotate').css('-webkit transform','rotate('+(-heading)+'deg');

如果(heading>=10&&heading=31&&heading我在你的代码中没有看到循环…我不得不想象它?添加计数器/触发器变量…如果不是循环,但是条件如果这就是你想说的。对不起,我还不熟悉循环的确切含义。请原谅我的业余表达。我认为它可能是一个循环,因为命令精简if是一次又一次地执行的。难道它不符合循环的条件吗?如果你不知道循环,你似乎在编程一些超出你应该做的事情……我认为你的ifs是在一个循环中,它会被多次触发。也许在一个被多次触发的函数中。我想补充一点,他会e为您的解决方案中的每个条件添加一个标志。仅当OP希望在单个循环中播放标记音频之外的其他音频时。问题不太清楚。OP希望每次变量改变值时播放一个声音,但仅播放一次以避免内存问题…只需要一个标志…来记录。想法是每个循环只播放一个声音condition.howler.js是为了提供尽可能多的节点而构建的。这就是为什么我可以播放同一声音的多个实例。OP希望有一个连续的循环,然后在
myVariable
值更改时播放不同的声音…这只是循环一次…我尝试过。现在发生了很多奇怪的事情。罗盘的每个功能超时10步。如果我在compass函数之外调整if逻辑的速度会怎么样?结束这项工作?这打开了另一个问题,即如何在compass函数之外读取航向变量?非常感谢您的关注。什么奇怪的东西?我认为您将无法实现任何真正的输出。从您的代码来看,您是eem将每隔30度播放一次声音。请尝试将计数器设置为30。抱歉,我不知道compass.js或compass在手机中的行为。它是否会触发角度变化?标题变量在哪里声明?要快速测试,请添加console.log(标题);就在compass watch函数之后。尝试阅读闭包以了解嵌套函数中的变量范围。谢谢JaredT。heading变量似乎是插件的一部分。它不是在我的代码中声明的。我想它是在compass.js中声明的。插件似乎在不断地寻找更改。这将d解释这种行为。我想到了另一种解决方案。由于我将学位号发送到HTML以显示它,我不妨添加一个事件侦听器来监视HTML元素的内容。它不会很优雅,但如果它能工作,我会很满意。你只会传递触发器。除非你限制触发器,否则它会我会很快发射。发生了什么奇怪的事情?我看不出代码有什么问题,可能是一些润色,比如如果方向改变,立即播放声音。你试过计数器并将其设置为360/(你有多少声音)?
$(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...
       });
});