The one you posted just has the animation and no JS, so we cannot see what you are doing…
i.e where you are placing this function which is normally in the head . Not sure what you expect from this ?
Can you supply the version with your JS in ??.
And explain what you are trying to do.
The reason this probably does not work is that it is fired too soon before the timeline is fully registered.
If you really want to add this to the Head then you would need to delay the code with something like a setTimeOut