The following code is working correctly for all browsers including Safari on Mac, with the exception of Safari on the iPhone.
I have a timer Object that could potentially be running that is defined like so:
//delay background change until animation is finished
lastTimer = setTimeout(function () {
$('#' + targetDiv).removeClass('open');
}, 150);
Later, I need to check if the timer is running, and if so cancel it. Here is the code I am using:
if (lastTimer != null) { clearTimeout(lastTimer); }
This is where IOS Safari generates the JavaScript Error:
ReferenceError: Can't find variable: lastTimer.
Any ideas on why the check for null is not preventing the error, like it seems to with the other browsers?
Here is the full code for the two pertaining functions in answer to the reply below: (edited with solution)
// 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);
}
}
}
When the error happens in iOS the execution stops and my test console.log
immediately after does not execute.