title: When are JavaScript objects destroyed?
body: <p>In C++ I can define a constructor and destructor explicitly, and then cout &lt;&lt; "C or D Called" from with in the constructor/destructor function, to know exactly where.</p>
  
  <p>However in JavaScript how do I know when an object is destructed.  The example below is the case that concerns me.</p>
  
  <p>I'm calling an internal function on a timeout and I'm wondering if the object stays alive as long as the timer is running, waiting to call next again.</p>
  
  <p><strong>User Click calls Control</strong></p>
  
  <pre><code>// Calls  Control
  </code></pre>
  
  <p><strong>Control calls Message</strong></p>
  
  <pre><code>var message_object = new Message( response_element );
  </code></pre>
  
  <p><strong>Message calls Effects</strong></p>
  
  <pre><code>new Effects().fade( this.element, 'down', 4000 );
  message_object.display( 'empty' );
  </code></pre>
  
  <p><strong>Effects</strong></p>
  
  <pre><code>/**
   *Effects - build out as needed
   *  element - holds the element to fade
   *  direction - determines which way to fade the element
   *  max_time - length of the fade
   */
  
  var Effects = function(  ) 
  {
      this.fade = function( element, direction, max_time ) 
      {
          element.elapsed = 0;
          clearTimeout( element.timeout_id );
          function next() 
          {
              element.elapsed += 10;
              if ( direction === 'up' )
              {
                  element.style.opacity = element.elapsed / max_time;
              }
              else if ( direction === 'down' )
              {
                  element.style.opacity = ( max_time - element.elapsed ) / max_time;
              }
              if ( element.elapsed &lt;= max_time ) 
              {
                  element.timeout_id = setTimeout( next, 10 );
              }
          }
          next();
      }
  };
  </code></pre>