title: How to execute something only once within if-loop?
body: <p>EDIT (more specific): </p>
  
  <p>I have the following situation. I am building an experimental synthesizer interface with JavaScript. I am using the <a href="http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library" rel="nofollow">howler.js</a> 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 <strong>heading of an iPhone compass</strong>. 
  <strong>I use <a href="http://ai.github.io/compass.js/" rel="nofollow">compass.js</a>. It has a variable called heading.</strong></p>
  
  <pre><code>$(function(){
  
      Compass.watch(function (heading) {
        $('.degrees').text(Math.floor(heading) + '°');
        $('#rotate').css('-webkit-transform', 'rotate(' + (-heading) + 'deg)');
  
         if (heading &gt;= 10 &amp;&amp; heading &lt;= 30) {
         sound.play("myFirstSound"); 
         } 
         else if (heading &gt;= 31 &amp;&amp; heading &lt;= 60) {
         sound.play("mySecondSound");
         } else { etc etc...
         });
  });
  </code></pre>
  
  <p>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 <strong>var heading does not exist</strong> error.</p>