I'm trying to create a bounce effect on an image after an synchronous AND an asynchronous call but can't figure out how to do it. The problem I'm having now is that I sometimes get both the bounce effect and afterwards isExecuted
becomes true because the asynchronous event takes some time.
My code should work like this:
Iterate over each object in myEnum
and execute the following
- if
myCondition1
is equal toomyCondition2
setisExecuted = true
- if above is not true, call an asynchronous method which evaluates some stuff, if it's true it will set
isExecuted = true
. - wait for all above is finished, then if
isExecuted
still is false, bounce the image.
Here's the code:
var isExecuted = false;
myEnum.each()
{
if (myCondition1 == myCondition2) { //Synchronous
isExecuted = true;
return false; //stop the iteration
}
else {
MyAsyncFunction(myCondition2, function(isTrue) { // Asynchronous
if (isTrue) {
isExecuted = true;
return false; //stop the iteration
}
});
}
});
// Execute this ONLY when above code is finished executing
if (isExecuted == false) {
BounceImage();
}
Please note that the async function is not always executed but the bounce check must always be executed if isExecuted
is true.