1

inside my demo function , i have a fadeout function. this function animate an image by fading it out and it runs for few seconds.

i want an alert to run only after this fading is completed. . it's not working this way. the alert is popping out before the fading is completed.

is there any way to sequence it beside calculating the runtime of each function and creating accordingly setTimeout for each command?

the Demo function:

function Demo() {
    fadeout(liran0);
    alert("hello");
}

the fadeout function is needed for analysis:

function fadeout(element) {
    var op = 1; // initial opacity
    var timer = setInterval(function () { //call the function every x milliseconds
        if (op <= 0.01) { // stop the function when the op is lower then 0.1 and then make in invisible.
            element.style.opacity = 0; // make it invisible
            clearInterval(timer); // clear the timer ....
            return true;
        }
        element.style.opacity = op;
        op -= 0.01;
    }, 10);
}
2
  • 2
    call alert in fadeout() just after clearInterval(), or pass a callback to fadeout(), or use a promise. Commented Jan 16, 2014 at 19:54
  • stackoverflow.com/questions/5289275/… Commented Jan 16, 2014 at 19:57

2 Answers 2

2

You're calling the alert immediately after kicking off the animation, which happens asynchronously. Instead do this:

function fadeout(element, finished) {
    var op = 1; // initial opacity
    var timer = setInterval(function() { //call the function every x milliseconds
        if (op <= 0.01) { // stop the function when the op is lower then 0.1 and then make in invisible.
            element.style.opacity = 0; // make it invisible
            clearInterval(timer); // clear the timer ....
            if (typeof finished === "function") {
                finished();
            }
            return true;
        }
        element.style.opacity = op;
        op -= 0.01;
    }, 10);
}

fadeout(liran0, function() {
    alert("hello");
});

The callback to fadeout isn't executed until the animation is complete.

Sign up to request clarification or add additional context in comments.

2 Comments

Updated with a callback type check
Ha! We wrote basically the same answer. +1 for great minds thinking alike.
0

If you take a look at the jQuery library does it, you will see that they always provide a callback function that gets called when the animation is finished running. Look at the slideUp() method, for example. Its second parameter takes a callback that is called when it is finished.

So, the approach here will be to call your callback after the clearInterval() call, like this.

function fadeout(element, complete) {
    var op = 1; // initial opacity
    var timer = setInterval(function () { 
        //call the function every x milliseconds
        if (op <= 0.01) { // stop the function when the 
                          // op is lower then 0.1 and then make in invisible.
            element.style.opacity = 0; // make it invisible
            clearInterval(timer); // clear the timer ....
            if ( typeof complete === 'function' ) {
                complete();
            }
            return true;
        }
        element.style.opacity = op;
        op -= 0.01;
    }, 10);
}

1 Comment

aside: typeof is an operator, but your code reads as though it's a function.

Your Answer

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.