title: Safari on IOS generates "ReferenceError: Ca ni n't find variable:"
body: <p>The following code is working correctly for all browsers including Safari on Mac, with the exception of Safari on the iPhone.</p>
  
  <p>I have a timer Object that could potentially be running that is defined like so:</p>
  
  <pre><code>//delay background change until animation is finished
  lastTimer = setTimeout(function () {
    $('#' + targetDiv).removeClass('open');
  }, 150);
  </code></pre>
  
  <p>Later, I need to check if the timer is running, and if so cancel it. Here is the code I am using:</p>
  
  <pre><code>if (lastTimer != null) { clearTimeout(lastTimer); }
  </code></pre>
  
  <p>This is where IOS Safari generates the JavaScript Error:</p>
  
  <blockquote>
    <p>"ReferenceError: Can't find variable: lastTimer".  </p>
  </blockquote>
  
  <p>Any ideas on why the check for null is not preventing the error, like it seems to with the other browsers?</p>
  
  <p>Here is the full code for the two pertaining functions in answer to the reply below: (edited with solution)</p>
  
  <pre><code>// Class for handling the animations for the drop down menus
  var dropDownMenu = {
  lastTimer: null,
  openMenu: function (targetDiv) {
      if (targetDiv != null) {
          var targetHeight = $('#' + targetDiv).height();
          $('#' + targetDiv).stop(true); //stop an previous animations and clear queue
          if (this.lastTimer != null) { clearTimeout(this.lastTimer); } //stop possible pending timer to prevent background change
          console.log("testing b");
          $('#mainNavigation #dropDownMenu ul').removeClass('open'); // make sure all closed menus show corrent bgd
          $('#' + targetDiv).animate({
              bottom: -(targetHeight + 30)
          }, 200, 'swing');
          $('#' + targetDiv).addClass('open');
      }
  
  },
  closeMenu: function (targetDiv) {
      if (targetDiv != null) {
          $('#' + targetDiv).stop(true); //stop an previous animations and clear queue
          $('#' + targetDiv).animate({
              bottom: 0
          }, 200, 'swing');
          //delay background change until animation is finished
          this.lastTimer = setTimeout(function () {
              $('#' + targetDiv).removeClass('open');
          }, 150);
      }
  }
  }
  </code></pre>
  
  <p>When the error happens in iOS the execution stops and my test <code>console.log</code> immediately after does not execute.</p>