title: Javascript: Is it ok to call cleartimeout before settimeout?
body: <p>I have a function that sets a timer, and calls itself back upon expiration of the timer. </p>
  
  <p>What I'd like to know is if it is bad practice to clear the timer at the top of the function.</p>
  
  <p>The reason for this is because I will be calling that function asynchronously from time to time, and if I don't clear the timer first, I'll have two running simultaneously. </p>
  
  <p>I realize that I can clear the timer right before I make the other call to the function, but I'm wondering if it will cause problems in any browser if I just keep the cleartimeout call inside the function which contains the timer. </p>
  
  <p>One other thought - Can I test the timer variable before making the cleartimeout call, to see if it is a timer?</p>
  
  <p>Here is some example code:</p>
  
  <pre><code>function onAir(){
  
      // reset timer  
      clearTimeout(timer);
  
      $.getJSON("http://mywebsite.com?format=json&amp;callback=?",function(data){
          if(data.result == '1'){
              do stuff here   
          }
          else{
              do other stuff here
          }   
      });
  
      // start timer
      timer = setTimeout("onAir()",60000);
  } 
  </code></pre>
  
  <p>Thanks for sharing your brain with me!</p>
  
  <p>Kenny</p>