问题描述
编辑(更具体):
我有以下情况.我正在使用JavaScript构建实验性的合成器界面.我正在使用 howler.js 插件处理音频.想法是,如果满足特定条件,则会播放声音.我有一个数字存储的变量,其值通过 iPhone指南针的方向改变.我使用 compass.js .它有一个称为标题的变量.
I have the following situation. I am building an experimental synthesizer interface with JavaScript. I am using the howler.js plugin to handle audio.The idea is that a sound is played if a certain condition is met. I have a number stored variable whose value changes via the heading of an iPhone compass.I use compass.js. It has a variable called 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标题不存在错误.
Even if heading is constant the .play command is executed not once but over and over again causing the audio engine to run out of memory. How can I execute the commands within the if loops only once? I can not access the value of heading outside the compass function. I always get var heading does not exist error.
推荐答案
采用布尔变量,如
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
In loop check the value of checkOnce. Loop will run till checkOnce=false
这篇关于如何在if循环中仅执行一次内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!