0

I would like to check the value of a variable inside my requestAnimationFrame function. I implement this value on the scroll wheel event. It seems to work but when I check the value inside my RAF fonction, it's still set at its first value.

I wonder how can I properly set my this.deltaY value ? Thank you very much

class MyClass
{
    constructor ()
    {   
        this.deltaY = 0;
    }

    init ()
    {
        this.playRAF();
        window.addEventListener('wheel', function(e) {
            this.deltaY = e.deltaY;

            console.log(this.deltaY); 
            // it returns a number
        });

    }

    playRAF ()
    {
        this.leRaf = requestAnimationFrame(this.playRAF.bind(this));
        
        console.log(this.deltaY); 
        // it returns 0 all the time despite the function above :(
    }
}

//////

const foo = new MyClass();
foo.init();
7
  • 2
    Use an arrow function here: window.addEventListener('wheel', e => {} Commented Jun 22, 2020 at 13:44
  • @adiga, thanks for your answer. Unfortunately, this issue is not related to what's inside my event listener but it's related on the value inside my playRAF function. Plus I don't want to use self since I will have several instances of my class. Commented Jun 22, 2020 at 13:51
  • Your event listener is definetely an issue. Without an arrow function, it will not set this.deltaY = e.deltaY to the instance. Since you have a this.playRAF() outside, I'm assuming init is called on the instance of the class. Commented Jun 22, 2020 at 13:58
  • Also, you probably need to call this.playRAF() INSIDE the event listener if you want that called every time wheel event occurs. Commented Jun 22, 2020 at 14:02
  • Thanks for your answer. playRAF() is a request animation frame function. So it is called all the time as soon as it is first call it. Yes I call my init() function with my instance. Since I changed my addEventListener with the arrow, my console.log(this.deltaY) returns "undefined", it seems it's not accessible anymore. Commented Jun 22, 2020 at 14:10

1 Answer 1

1

This is an this issue

pay attention to this block, in the event listener callback, this is window, instead of the class instance as you thougt it is.

window.addEventListener('wheel', function(e) {
  this.deltaY = e.deltaY;

  console.log(this.deltaY); 
  // it returns a number
});

try to check the value window.deltaY, you will find that it's your logged number, or try a breakpoint debugging, the this in the function(e){} is Window object.

turn it into

window.addEventListener('wheel',(e) => {
  this.deltaY = e.deltaY;

  console.log(this.deltaY); 
  // it returns a number
});

will solve this problem, as the arrow will not create a function block, this will be the class instance as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.