Question

I have a function that handles scrolls every period of time. This function works when users...

I have a function that handles scrolls every period of time. This function works when users scroll with mouse wheel:

let shouldHandle = true
window.addEventListener('wheel', e => {
    if (shouldHandle) {
        handleScroll(e) // I will handle scrolls here
        shouldHandle = false
        setTimeout(() => {
            shouldHandle = true
        }, 750)
    }
})

However, when I am scrolling using my laptop's touchpad, scroll still happens even when I remove my finger (especially when I accelerate my finger enough and scroll, then immediately remove my finger from the touchpad). As a result, scroll still happens after the 750ms even when the users are not technically scrolling. This question has been asked here. The question did not receive an answer to handle this behavior.

I want to handle scroll only after a period of time has elapsed from the last scroll. The scrolls I want to handle must not be because of scrolling caused by this "predictive touch" scroll. Is there a way to achieve this as of now?

The programming language is Javascript

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: used code provided in the question directly.

The only way that comes to mind is to watch scrollY for a period after you've seen a scroll start and wait for it to stabilize and only then consider that scroll "completed" and start your 750ms timer. Constantly polling would be time taking and inefficient, but doing it for a short period while the scrolling is still actively occurring seems acceptable.

//Rough Code

    let shouldHandle = true
    let lastScollY = null
    let lastScrollTimer = 0
    window.addEventListener('wheel', e => {
        if (shouldHandle) {
            handleScroll(e) // It will handle scrolls here
            shouldHandle = false
            waitForScrollToEnd(() => {
               setTimeout(() => {
                  shouldHandle = true
               }, 750) // or perhaps 700 on the basis that upto 50 was spent in  'waitForScrollToEnd'
           })
        }
    })


    function waitForScrollToEnd(cb) {
        clearTimeout(lastScrollTimer)
        lastScrollY = window.ScrollY
        lastScrollTimer = setTimeout(poll, 50)
        function poll() {
            if (lastScrollY == window.scrollY) {
                lastScrollY = null
                lastScrollTimer = 0
                cb()
            }
            else {
                lastScrollY = null
                lastScrollTimer = setTimeout(poll, 50)
            }
        }
    }

Add a comment
Know the answer?
Add Answer to:
I have a function that handles scrolls every period of time. This function works when users...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Dr. Victoria van der Walt has to decide how to handle a complaint letter from a...

    Dr. Victoria van der Walt has to decide how to handle a complaint letter from a customer. When she received the letter, she passed it along to Paul Zimbalist, the firm’s customer ser- vice manager, to get his recommendation. Now van der Walt has a reply from Zimbalist, and she must decide how to re- spond to the customer and determine if changes are needed in her company’s customer service operations. Dr. van der Walt has a reputation as a...

  • Response Questions Part A To C A) Did the City of Dixon have the appropriate internal...

    Response Questions Part A To C A) Did the City of Dixon have the appropriate internal controls? What elements do you think they fell short in? B) Who should have detected the fraud first? and why? C) Please share your thoughts about information below and what surprised you most about the information below? Information Welcome to Dixon Illinois about an hour and a half west of bustling Chicago but it has the genteel feel of a small town from the...

  • Write 6-7 Sentences For Your Response: Did the City of Dixon have the appropriate internal controls?...

    Write 6-7 Sentences For Your Response: Did the City of Dixon have the appropriate internal controls? What elements do you think they fell short in? Information Welcome to Dixon Illinois about an hour and a half west of bustling Chicago but it has the genteel feel of a small town from the Dixon arch, a lucky horseshoe that hangs over the quaint community to the bright white boyhood home of Ronald Reagan. Reagan isn't just Dixon's claim to fame, he's...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • What are two internal factors for a company in the article below and is the factor...

    What are two internal factors for a company in the article below and is the factor a strength or weakness for the company? Explain. Please include internal factors such as SWOT analysis, core competence, Resources and capability analysis. Based on what you know about the company, which generic company strategy makes sense? Why? What are the pitfalls to avoid for this strategy? SWOT analysis is a strategic planning technique used to help a person or organization identify strengths, weaknesses, opportunities,...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

  • For the preparation and standardization of NaOH with KHP im supposed to boil water for 1hr and 30 min to remove CO2

    For the preparation and standardization of NaOH with KHP im supposed to boil water for 1hr and 30 min to remove CO2....the problem is that if I don't boil it for that long and (30 min) b/c of not enough time but I put the water I boiled for 1/2 hr aproximately into a NaOH bottle with a CO2 absorber and stored it there for a few days. I would assume that I would have to boil the water again...but...

  • I have this case study to solve. i want to ask which type of case study...

    I have this case study to solve. i want to ask which type of case study in this like problem, evaluation or decision? if its decision then what are the criterias and all? Stardust Petroleum Sendirian Berhad: how to inculcate the pro-active safety culture? Farzana Quoquab, Nomahaza Mahadi, Taram Satiraksa Wan Abdullah and Jihad Mohammad Coming together is a beginning; keeping together is progress; working together is success. - Henry Ford The beginning Stardust was established in 2013 as a...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • 10. The Beck & Watson article is a Group of answer choices quantitative study qualitative study...

    10. The Beck & Watson article is a Group of answer choices quantitative study qualitative study 11. Beck & Watson examined participants' experiences and perceptions using what type of research design? Group of answer choices particpant obersvation phenomenology 12. Select the participants in the Beck & Watson study Group of answer choices Caucasian women with 2-4 children Caucasian pregnant women 13. In the Beck & Watson study, data was collected via a(n) Group of answer choices internet study focus group...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT