Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
JQuery, run function one at a time on scroll

So, I bind a function to window scroll, but it runs repeatedly when user scrolls.

But I want it to run once when user scrolls, and when the scroll ends, then when user scrolls again I want it to run again.

Right now my code looks like this:

function whenScroll() {
    var zSection = $(".zSection").toArray();

    if (window.calc == -1) {
        $(zSection[window.zSecPos]).stop().slideToggle();
    } else {
        $(zSection[window.zSecPos-1]).stop().slideToggle();
    }
    clearTimeout($.data(this, "scrollTimer"));
    $.data(this, "scrollTimer", setTimeout(function() {
        if (window.calc == -1) {
            window.zSecPos = window.zSecPos + 1;
        } else {
            window.zSecPos = window.zSecPos - 1;
        }
    }, 250));
}

window.zSecPos = 0;
$(window).scroll(function() {
    whenScroll();
});

By the way, window.calc indicates the scroll direction, which I determine in another js file.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I ended up fixing it myself. Sorry.

The thing is I have seperate all page long sections, which I want to scroll through between them.

So, my final code:

function whenScroll() {
    var zSection = $(".zSection").toArray();

    clearTimeout($.data(this, "scrollTimer"));
    $.data(this, "scrollTimer", setTimeout(function() {
        if (window.calc == -1) {
            if ($(zSection[window.zSecPos+1]).length) {
                $(zSection[window.zSecPos]).stop().animate({
                    top: "-100vh"
                }, 300);
                window.zSecPos = window.zSecPos + 1;
            }
        } else {
            if ($(zSection[window.zSecPos-1]).length) {
                $(zSection[window.zSecPos-1]).stop().animate({
                    top: "0"
                }, 300);
                window.zSecPos = window.zSecPos - 1;
            }
        }
        console.log(window.zSecPos);
    }, 300));
}

$(document).ready(function() {
    setTimeout(function() {
        $(".zSection").removeClass("displayNone");
    }, 1000);
});

window.zSecPos = 0;
$(window).scroll(function() {
    whenScroll();
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!