title: How do callbacks work in JavaScript?
body: <p>I've come across an example in my code that lead me to revisiting callbacks in Javascript, which I was always shaky on. </p>
  
  <p>The example: I need function "type" to execute and finish before function "erase". I could add some timer to type that ensures it ended, but I'd like to find a better way. </p>
  
  <p>Digging into type:</p>
  
  <pre><code>function type() {
      captionEl.html(caption.substr(0, captionLength++));
      if(captionLength &lt; caption.length+1) {
          setTimeout('type()', 50);
      } else {
          captionLength = 0;
          caption = '';
      }
  }
  </code></pre>
  
  <p>The function isn't ended until caption = " " (nothing") - thats when I'd like to see erase() run. </p>
  
  <p>So, do I pass callback as a parameter in type (like type(callback)) and then execute callback where caption is set to nothing? But how do I 'test' the type function to have caption as ""?</p>