0

I have the following code:

function newGraphic() {
 $.post('./lexiconGraph.pl', {
 'tag' : this.getAttribute('apolo'),
 'apoloKey' : this.id,
 'h' : surface.h,
 'w' : surface.w,
 'operation' : 'newGraphic'
 }, function(result) {
 minGraphic();
 // STOP AND WAIT //
 document.getElementById('container').innerHTML = result;
 getNodes();
 // STOP AND WAIT //
 maxGraphic();
 });
}
function minGraphic() {
 if (z > 0.01) {
 zoom(parseFloat(z - 0.01));
 setTimeout('minGraphic();', 5);
 }
}

The problem is: The function minGraphic creates an "animation effect" of unknown duration (depends on the size of the graph). I need to find a way to make the following lines are executed only when the function minGraphic finished.
Does anyone have any idea how I can do this?


NOTE: Put the following lines in the function minGraphic is not an option, since I already use this function elsewhere.

azz
5,9703 gold badges33 silver badges60 bronze badges
asked Sep 24, 2013 at 13:40
5
  • If you cannot edit the function minGraphic, then providing a solution also appears to be not an option. Commented Sep 24, 2013 at 13:52
  • @Adam This is not true. See my answer. Commented Sep 24, 2013 at 14:02
  • @DerFlatulator - what answer? Commented Sep 24, 2013 at 14:53
  • @Adam, It was removed after I indicated that it wouldn't be working. Commented Sep 24, 2013 at 15:20
  • That's what I get for answering questions way after my bedtime Commented Sep 24, 2013 at 22:24

2 Answers 2

1

Use a callback for when the animation is complete. Something like this:

function newGraphic() {
 $.post('./lexiconGraph.pl', {
 'tag' : this.getAttribute('apolo'),
 'apoloKey' : this.id,
 'h' : surface.h,
 'w' : surface.w,
 'operation' : 'newGraphic'
 }, function(result) {
 var afterAnimation = function()
 {
 document.getElementById('container').innerHTML = result;
 getNodes();
 maxGraphic();
 };
 minGraphic(afterAnimation);
 });
}
function minGraphic(cb) {
 if (z > 0.01) {
 zoom(parseFloat(z - 0.01));
 setTimeout(function() { minGraphic(cb); }, 5);
 }
 else
 {
 if(cb) cb();
 }
}

If I'm following correctly, what this will do is trigger the start of miniGraphic, passing in a callback to a function that will execute after the animation is complete (i.e. z is not> 0.01). When that event happens, if a callback (cb) was passed in, it will call that function, thus triggering the next step in the animation.

If you have stuff you need to wait on for the maxGraphic method, then you can adopt the same concept.

Alternatively, if jQuery is available to you, jQuery has built-in ways for post-animation callbacks that you can use. See this documentation

answered Sep 24, 2013 at 13:48
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a callback?

function minGraphic(callback) {
 if (z > 0.01) {
 zoom(parseFloat(z - 0.01));
 setTimeout($.proxy(minGraphic, this, callback), 5);
 return;
 }
 callback && callback();
}

Then just call the function like:

minGraphic(function () {
 //complete
});

Note that the callback argument is optionnal to stay backward compatible with previous code not interested in knowing when the operation was completed.

answered Sep 24, 2013 at 13:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.