title: Toggle One And Off - JavaScript - Web Audio API
body: <p>I am an Audio Engineer and just started JavaScript and HTML5, Specifically WEB Audio API.</p>
  
  <p>I have this oscillator code which is connected to destination(output).
  here is the code</p>
  
  <p>I want to have a buttons to connect and disconnect the oscillator to destination
  I managed to make it start but I can't disconnect it.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;script&gt;
          //creating the context
          context = new webkitAudioContext(); //allways the first code for audio API
          function osc1(frequency){ // Creating a function that has an oscillator going to gainNode and then going to the destination
  
              //creating AudioNodes and AudioParams
  
              //creating OscillatorNode
              var oscillator = context.createOscillator(); //creating Node's as Variables
              oscillator.type = 0; //0 is a sine wave
              oscillator.noteOn(0); // turning on the oscillator
              oscillator.frequency.value = frequency;// telling that frequency in () of the function equals to what
  
  
              //creating GainNode
              var gain = context.createGainNode(); // creating the Gain node
              gain.gain.value = 1; // setting up the value for gain node
  
  
              //Making the connections
              oscillator.connect(gain); // connecting oscillator to gain
              gain.connect(context.destination); // connecting gain to destination (speakers)
          }; // now we have a function called osc1(we can put frequency in here) then we can re call
  
  
      &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;input type="button" value="on"  onClick="osc1(500);" /&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>I know that the code for disconnecting it is <code>oscillator.disconnect();</code>, but I don't know how to to execute it.</p>