Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:13:09+00:00 2026-06-14T01:13:09+00:00

I was reading an article on HTML5Rocks that gave an example about scrolling through

  • 0

I was reading an article on HTML5Rocks that gave an example about scrolling through a webpage and checking an array of DOM elements offsetTop’s to see if they should be visible.

The article says the best practice way of doing this would be to update a variable with the windows current offset top every time a scroll event is fired. When the first scroll event is fired, it triggers the requestAnimationFrame process of checking offsetTop’s of the DOM elements. This decouples the visibility logic from the scroll event.

While I understand the benefit of certainly decoupling these two processes (since the scroll event could be called hundreds of times a second), I can’t see the benefit of running the visibility logic every 16ms after the first scroll event, regardless of whether the user has continued to move or not..

Can someone please explain what part of the process I’m missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:13:10+00:00Added an answer on June 14, 2026 at 1:13 am

    I think it’s well explained in the article.

    What else can we do? Well for one thing we are constantly running
    requestAnimationFrame and that’s not necessary if we haven’t just
    scrolled since nothing will have changed. To fix that we have the
    onScroll initiate the requestAnimationFrame

    Now whenever we scroll we will try and call requestAnimationFrame, but
    if one is already requested we don’t initiate another. This is an
    important optimization, since the browser will stack all the repeated
    rAF requests and we would be back to a situation with more calls to
    update than we need.

    Thanks to this setup we no longer need to call requestAnimationFrame
    at the top of update because we know it will only be requested when
    one or more scroll events has taken place. We also no longer need the
    kick off call at the bottom, either, so let’s update accordingly:

    var latestKnownScrollY = 0,
        ticking = false;
    
    function onScroll() {
        latestKnownScrollY = window.scrollY;
        if (!ticking) {
            requestAnimationFrame(update);
        }
        ticking = true;
    }
    function update() {
        ticking = false; // reset the tick so we can capture the next onScroll
    
        var currentScrollY = latestKnownScrollY;
    
        // Do visibilty logic and animation here
    }
    

    So, "regardless of whether the user has continued to move or not" is not really true. update is only called during (or a littlebit after) the scroll, and at a browser-choosen frame rate instead of a rate of hundreds of events per seconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading an article about how query expressions defer executions. Does that mean
Reading an article about HTML5, it occurs to me that while placeholders are incredibly
I was reading an article about non-type template arguments, and it said that :
I was reading this article about Dynamic Objects in C# 4.0 . In that
After reading this article on thedailywtf.com, I'm not sure that I really got the
After reading an article about fonts, i tried the code and i am getting
I am reading an article in IEEE Computer magazine about using data mining on
I was reading this article by Brandon Aaron here , about how jquery context
i was reading an article about smartphones and features phones, and i was surprised
I was reading an article here: http://javascriptweblog.wordpress.com/2010/03/16/five-ways-to-create-objects/ It tells about five ways of creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.