Jump to content
Search Community

Scrolltrigger killAll is buggy?

Basilico test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

While I was working on a project I did some test logging the ScrollTrigger.killAll internal loop and I found that the forEach loop simply stops earlier.
Even avoiding the "ScrollSmoother" test and just killing every "t", the loops ends before the end.
And I found it really strange!

 

_triggers.forEach(function (t) {
return t.vars.id !== "ScrollSmoother" && t.kill();
});


But if I get the triggers with getAll and then loop them, it works.

 

Link to comment
Share on other sites

Sure. Internally, ScrollTrigger maintains an Array of all the instances. When you call kill() on a particular instance, it removes itself from that Array. I mistakenly thought that a .forEach() would not be affected the way that a normal for... loop would be. For example: 

let triggers = [0,1,2,3],
    length = triggers.length;

for (let i = 0; i < length; i++) {
  triggers.splice(i, 1); // leads to skipping because the index shifts
}

The first time through the loop, i is 0, so it'd remove that...but now what WAS in the second slot is now in the first slot. When we iterate i in the loop, now the second time through the loop i is 1 but that is actually referring to what WAS in the 3rd slot! See the issue? For some reason, I believed .forEach() made sure that every element went through the loop rather than being index-dependent like the normal for... loop. 

 

So the solution is very simple: 

// old: 
triggers.forEach(...);
                 
// new:
triggers.slice(0).forEach(...);

Does that clear things up? 

  • 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...