Jump to content
Search Community

Arrow function in onComplete

yannick test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi, how can someone use a arrow function as a onComplete handler?

 

The arrow function won't get executed in the example below.

this.gmapTl = new TimelineMax({ paused: true, onComplete: onCompleteFn })...
const onCompleteFn = () => {
   console.log(this.gmapBlurred);
   this.gmapBlurred = !this.gmapBlurred
   console.log(this.gmapBlurred);
}

 

Although this works fine.

 

this.gmapTl = new TimelineMax({ paused: true, onComplete: () => {
      console.log(this.gmapBlurred);
      this.gmapBlurred = !this.gmapBlurred
      console.log(this.gmapBlurred);
   } })...

 

 

 

Link to comment
Share on other sites

Your onCompleteFn is an anonymous function, so it doesn't get hoisted like a named function. Try declaring onCompleteFn first.

 

const onCompleteFn = () => {
   console.log(this.gmapBlurred);
   this.gmapBlurred = !this.gmapBlurred
   console.log(this.gmapBlurred);
}

this.gmapTl = new TimelineMax({ paused: true, onComplete: onCompleteFn })

 

  • Like 1
Link to comment
Share on other sites

Alternate way using a named function.

 

this.gmapTl = new TimelineMax({ 
  paused: true, 
  onComplete: onCompleteFn,
  callbackScope: this
})

function onCompleteFn() {
   console.log(this.gmapBlurred);
   this.gmapBlurred = !this.gmapBlurred
   console.log(this.gmapBlurred);
}

 

  • Like 1
Link to comment
Share on other sites

Thanks for the answer. The first solution works great. Thanks a lot.

 

I'm using this timeline in a Vue method. In the named function this is not bound to the component. That's why I want to use the arrow function.

  • Like 1
Link to comment
Share on other sites

9 minutes ago, yannick said:

In the named function this is not bound to the component. 

 

It should be. Notice the 3rd parameter in the timeline, callbackScope. Setting it to 'this' should give it the correct scope.

 

But yeah, the first method is fine. Just pointing out the difference in case anybody else comes across this.

 

 

  • 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.
×
×
  • Create New...