Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often does .scroll() fire?

I am building my website here: argit.bounde.co.uk

I understand that it is bad to use $(window).scroll(); as that can cause problems in some browsers and have read at this blog post about the problems it can cause.

I have a function navCheck which consists of two parts, fixing the navigations (if you resize the browser to make it narrower you will see the second) and selecting which nav should occupy the "over" state.

Im quite new to jQuery and couldnt implement John's method, it didnt like where I put the variables. So I thought I would include a setInterval with my navCheck function to control the amount of times it fires. But I cant afford having the interval any greater than 1ms for the left nav fixing functions otherwise there is an obvious jump.

I wondered how often .scroll() fires by default and what would be the best option to pursue. Should I move the fixing part of my function to a separate function and have that fire every 1ms and then keep the "over" part of my function and have that fire every 250ms? or just keep the whole things firing every 1ms? and is that even a performance increase over the standard .scroll()

Or equally if you could help me implement Johns solution that would also be a step forward as that should be better still but I dont understand how I would implement it.

like image 967
rbyrne Avatar asked Nov 13 '22 07:11

rbyrne


1 Answers

You don't really need the count. The answer is "a lot", and implementation-specific.

Your goal should likely be to set state with the scroll, and then check that state in a loop, using setTimeout.

If your functions are lean, inside of scroll|resize|mousemove|etc there's nothing stopping you from tying to them.

But when I say "lean", I mean

window.onscroll = function (e) {
    screen_obj.x = ...;
    screen_obj.y = ... ;
};

Do your polling outside of that. Set it for 60fps, or whatever you'd like.
For the record, several browsers won't actually give you 1ms, they'll just give you their smallest interval.

like image 85
Norguard Avatar answered Nov 14 '22 22:11

Norguard