Skip to content Skip to sidebar Skip to footer

JQuery Function Running When It Shouldn't

I have a function which only needs to run when the width is less then a specific value. I have done this with the if ($(window).width() < n) { } but the function also runs when

Solution 1:

Instead of just if (windowsize < 1000) you also need to know the current status.

if (is_large && windowsize < 1000)
{
    is_large = false;
    // rest of code to handle transition to small size
}
else if (!is_large)
{
    is_large = true;
    // code to handle transition to large size
}

You also need to handle the case of setting is_large on page load before the initial call to checkWidth().


Post a Comment for "JQuery Function Running When It Shouldn't"