Jump to content
GreenSock

Igor

Simple way set scope (delayedCall/killTweensOf)

Recommended Posts

Hello! Is it possible to easily set scope without storing additional variables?

For example in pixi.js I can do:

target.on('event', this.func, this); // this code inside the class. Last argument is scope (instance of class).

target.off('event', this.func, this); // no need to set id or store additional variables. Just also specify scope.

 

given that there is a lot of code in classes and often you need to specify the context, I think it's really more convenient, than:

  1. tween = gsap.delayedCall(1, this.func.bind(this)); gsap.killTweensOf(tween) // need to store a variable
  2. gsap.delayedCall(1, this.func.bind(this)).vars.id = 'id'; gsap.killTweensOf('id') // need to come up with id

 

I would like to see:
gsap.delayedCall(1, this.func, this);

gsap.killTweensOf(this.func, this);

Thanks for help:)

 

Link to comment
Share on other sites

Hi Igor,

 

I would just bind the method ahead of time, like in the constructor.

this.func = this.func.bind(this);

...

gsap.delayedCall(1, this.func)

 

Or use an arrow function.

gsap.delayedCall(1, () => this.func());

 

  • Like 1
Link to comment
Share on other sites

Also, you can use a function as a property and there is no need to bind.

 

class Foo {
  
  func = () => {
    console.log("THIS", this)
  }
  
  constructor() {
    this.msg = "hello"
    
    gsap.delayedCall(1, this.func) // works
  }  
}

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×