Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  147] [ 3]  / answers: 1 / hits: 18992  / 12 Years ago, wed, june 13, 2012, 12:00:00

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.


More From » ios

 Answers
34

I want to chime in on this to explain. Mobile Safari is less forgiving when checking for undefined using the simple check,



if variable


When you come across situations like this then use,



if typeof variable === undefined


Attaching the variable to this is one solution here but it's just taking advantage of the fact that definedVariable.undefinedProperty returns undefined, whereas referencing an undefined variable directly will cause the reference error in some runtime environments.



I would advise not getting into the habit of attaching to this if it's not necessary.


[#84930] Tuesday, June 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;