I have the following code, that changes the class of a div when you scroll down. The problem is if you scroll very fast (or if I put links to a div in my menu) the timeout function doesn't execute just the last condition but all of them as a sequence. What I want to do is if the condition changes while the function is within the timeout to skip the function and check for the next condition.
$(document).scroll(function() {
var about = jQuery('#hh1').position().top;
var portfolio = jQuery('#hh2').position().top;
var services = jQuery('#hh3').position().top;
var workingprocess = jQuery('#hh4').position().top;
var clients = jQuery('#hh5').position().top;
var blog = jQuery('#hh6').position().top;
var contact = jQuery('#hh7').position().top;
var scroll = $(this).scrollTop();
if (scroll >= hh1-90 && scroll < hh3-90 || scroll >= hh5-90 && scroll < hh6-90)
{setTimeout('$(".div").addClass("MyClass")',3440);}
else
{
setTimeout('$(".div").removeClass("MyClass")',3440);
}
});