Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  6] [ 5]  / answers: 1 / hits: 28696  / 13 Years ago, wed, august 24, 2011, 12:00:00

timer_gear exist only in case if I press some button (until 5 sec). But there is another function it can be called any time. In this function I clear the timer and restart it. But first I have to check if the object exists otherwise I get this error:
Uncaught ReferenceError: timer_gear is not defined



Could you help me to solve this? These does not work.



if(timer_gear!=undefined)clearTimeout(timer_gear);

if(timer_gear)clearTimeout(timer_gear);


EDIT1:
first I misspelled my question: if(!timer => if(timer
EDIT2:



the full code is:



function hide_gear(){
$('#gear_div').animate({opacity: 0}, 1000);
delete timer_gear; //EDIT3: destroy object
}


...



/*gear*/
$('#gear').click(function(){
$('#gear_div').animate({
opacity: 1,
}, 1000, function() {
timer_gear = setTimeout(hide_gear();,5000);
});
});
$('#gear').mousemove(function(){
if( ? ? ? )
{
clearTimeout(timer_gear);
timer_gear = setTimeout(hide_gear();,5000);
}

});


Results:



timer_gear// Uncaught ReferenceError timer_gear is not defined
timer_gear != undefined // Uncaught ReferenceError: timer_gear is not defined
typeof timer_gear !== undefined // WORKS
typeof timer_gear != undefined // WORKS, just tired it
var timer_gear; //at the begining - WORKS, but I did not wanted a new variable if its not necessary


thank you for answers!


More From » javascript

 Answers
12

The 1st one should be:



if(typeof timer_gear !== undefined){
clearTimeout(timer_gear);
}


And the 2nd, but this won't work if timer_gear is not defined, so you should use the typeof one above:



if(timer_gear){
clearTimeout(timer_gear);
}

[#90439] Tuesday, August 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;