title: Can calling setTimeout in a closure cause a memory leak?
body: <p>I have a third-party JavaScript object on which I’m calling a method “search” and passing in a search query string along with a function to execute once the search is complete. This third-party object then goes away and attempts to retrieve data from a service. If the service call fails to return within 10 seconds, the third-party object logs a timeout error but unfortunately doesn’t execute the callback function with a status of “Timeout” or something else applicable.</p>
  
  <p>In order to have the callback execute when there has been a timeout, I’ve wrapped the third-party object call as per the code below:</p>
  
  <pre><code>var SEARCH_TIMEOUT_MILLISECONDS = 10500;
  
  var thirdPartyObject = ... // Global variable
  
  function search(searchTerm, onCompleteCallback) {
  
      var searchTimeoutHandler = setTimeout(function () {
          onCompleteCallback(null, 'TIMEOUT');
      }, SEARCH_TIMEOUT_MILLISECONDS);
  
      thirdPartyObject.search({
          searchTerm: searchTerm,
          onComplete: function (searchResponse, status) {
  
              clearTimeout(searchTimeoutHandler);
  
              onCompleteCallback(searchResponse, status);
          }
      });
  }
  </code></pre>
  
  <p>Where a closure is being created, if this function was called hundreds of times (some concurrently), would there be any memory issues in the scenarios of the setTimeout function either being or not being called?</p>