The value of coordX
isn't set until onComplete
is actually called after the first .to
completes. Until then the value is undefined
.
So the problem is that at definition time, the value of coordX
is undefined
, and that is the value you are assigning to left
. So when that .to
is evaluated, it sees an undefined
value.
Perhaps a better way is to initiate the reverse animation within onComplete
:
function animationStart() {
tl.to("#mydiv", 0.5, {
borderRadius:"50%",
width: "35%",
onComplete: function() {
var coordX = regPosition();
tl.to("#mydiv", 0.5, {left: 1000})
.fromTo("#mydiv", 1.5, {left:-800}, {left: coordX});
}
})
}