Samdeesh
Samdeesh

Reputation: 945

Javascript: How can we combine events?

I am trying to scroll one page at any scroll event but, multiple scroll events are triggered when we use mouse wheel in my example.

I want to scroll one screen at a time when mouse or keyboard is used to scroll but the event as below is triggered multiple times for mouse wheel scroll

window.onscroll = function (e) {
    e.preventDefault();}

Upvotes: 1

Views: 619

Answers (1)

vinayakj
vinayakj

Reputation: 5681

You can use a setTimeout to detect the end of scroll event and then run your code like below.

var t;
addEventListener('scroll', scrollEndFunc)
function scrollEndFunc(e) {
    clearTimeout(t);
    t = setTimeout(function(){
            //todo stuff
        }, 600) // wait untill 600ms, if no scroll then
                // execute actual function else cancel it.
}

Here is the Edit to codepen

Upvotes: 2

Related Questions