Jump to content
Search Community

onUpdate only called once

jiggy1965 test
Moderator Tag

Go to solution Solved by Dipscom,

Recommended Posts

I'm trying to use an external function which onUpdate could call:

<!doctype html>
<html lang="en">
<head>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/gsap.min.js"></script>
</head>
<body>
	<div id="slider"></div>

	<script>

		var tl = gsap.timeline();
		var value = {startvalue:50};
		var targetvalue = 100;
		tl.to(value, {startvalue:targetvalue, duration:2, ease: Power1.easeOut, onUpdate: updateslider(value.startvalue) });

		function updateslider(x) {
			console.log(x);
		};

	</script>

</body>
</html>

The problem is that this onUpdate is only called once. I get to see  '50', the starting value, but it doesn't go up to '100'.

 

When I don't use an external function and put the function inside onUpdate it works as expected though and I see the number go up from 50 to 100:

<!doctype html>
<html lang="en">
<head>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/gsap.min.js"></script>
</head>
<body>
	<div id="slider"></div>

	<script>

		var tl = gsap.timeline();
		var value = {startvalue:50};
		var targetvalue = 100;
		tl.to(value, {startvalue:targetvalue, duration:2, ease: Power1.easeOut, onUpdate: function(){console.log(value.startvalue)} });

	</script>

</body>
</html>

 

Shouldn't it be possible to have onUpdate execute an external function with every update?

 

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Sure, no problem.

So when I use an external function for onUpdate that function is only called once an in Console I only get to see 50 without it going up:
https://codepen.io/coder1965/pen/eYMvybq?editors=1111

 

When I put the same function inside the onUpdate function however it works as expected. In the Console I get to see 50 go up to 100:
https://codepen.io/coder1965/pen/poLepGY?editors=1111

Link to comment
Share on other sites

  • Solution

Hey jiggy1965!

 

That is happening because you're calling the extenal function instead of asigning it to the onUpdate handler.

 

// This calls the function once and assign its return value to onUpdate:

{
  onUpdate: myFunction()
}

// This assigns myFunction as the method to call whenever onUpdate runs:

{
  onUpdate: myFunction
}

Now, on your case, if you want to pass some values to the function being called onUpdate, all you need to do is wrap your function into an empty function like:

 

tl.to(value, {startvalue:targetvalue, duration:2, ease: Power1.easeOut, onUpdate: () => updateslider(value.startvalue) });

 

Happy tweening!

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