Delay a javascript function until a css animation is complete - Stack Overflow

I am trying to make a modal object library that will create and open a div, iframe, img, similar to col

I am trying to make a modal object library that will create and open a div, iframe, img, similar to colorbox. I am doing this in pure javascript, so please do not remend jQuery.

The problem is, when a user creates a new modal using var myModal = new modal(options, width, height), I want it to check if a modal already exists, close it, wait for the close animation, then continue to create the new modal. I can already do everything, but I'm having an issue waiting to create the new modal until the old one is gone. I am aware of webkitTransisionEnd and firing custom events, but that is not the issue. I need the actual code to wait until the old modal is finished closing until it continues on to finish the rest of the function and still return the correct object to the user. Here are some of the things I've tried:

  • Creating a transisionEnd listener waiting for the animation to end then creating the new modal. (this worked but considering it then bees a nested function, it's hard to return the correct object).
  • Using a try, catch block. (this didn't work for my purposes)
  • Using a countless number of variations of the same thing where I use recursive functions

If anyone has ideas, please feel free to post them. I have tried a lot of things, but apparently not the one thing that I need to. Thanks.

EDIT:

I was able to figure it out. All I had to do was attach a transitionEnd listener to the modal that is already open, then create an additional function outside of the class that would then recall the modal with the same constructor. The code looks a bit like this:

function create(options, width, height) {
    return new modal(options, width, height);
}

function modal(options, width, height) {
    if (modal != null) {
        modal.close();
        modal.addEventListener('webkitTransitionEnd', function() {
            create(options,width,height);
        });
    }
    return;
}

I am trying to make a modal object library that will create and open a div, iframe, img, similar to colorbox. I am doing this in pure javascript, so please do not remend jQuery.

The problem is, when a user creates a new modal using var myModal = new modal(options, width, height), I want it to check if a modal already exists, close it, wait for the close animation, then continue to create the new modal. I can already do everything, but I'm having an issue waiting to create the new modal until the old one is gone. I am aware of webkitTransisionEnd and firing custom events, but that is not the issue. I need the actual code to wait until the old modal is finished closing until it continues on to finish the rest of the function and still return the correct object to the user. Here are some of the things I've tried:

  • Creating a transisionEnd listener waiting for the animation to end then creating the new modal. (this worked but considering it then bees a nested function, it's hard to return the correct object).
  • Using a try, catch block. (this didn't work for my purposes)
  • Using a countless number of variations of the same thing where I use recursive functions

If anyone has ideas, please feel free to post them. I have tried a lot of things, but apparently not the one thing that I need to. Thanks.

EDIT:

I was able to figure it out. All I had to do was attach a transitionEnd listener to the modal that is already open, then create an additional function outside of the class that would then recall the modal with the same constructor. The code looks a bit like this:

function create(options, width, height) {
    return new modal(options, width, height);
}

function modal(options, width, height) {
    if (modal != null) {
        modal.close();
        modal.addEventListener('webkitTransitionEnd', function() {
            create(options,width,height);
        });
    }
    return;
}
Share Improve this question edited Feb 8, 2013 at 4:15 Rook777 asked Feb 8, 2013 at 1:02 Rook777Rook777 1512 silver badges7 bronze badges 2
  • Are you saying when you call "new modal(...)" you want to destroy the old modal? Or, do you want the new modal to "bee" the old modal? What modal instance do you want to return to the user from "new modal(...)"? – joeltine Commented Feb 8, 2013 at 1:06
  • 1 @joeltine I want to destroy the old and create a new. And I would want to return the new modal instance. – Rook777 Commented Feb 8, 2013 at 3:51
Add a ment  | 

2 Answers 2

Reset to default 3
var animationDuration = 1000;
setTimeout(function(){
    // Animation done!
}, animationDuration);

You can't cause code to wait (e.g. pause execution of the current thread of execution) until some future event occurs. Javascript simply does not support that or work that way. It does not have a way to block the current thread of execution other than a couple modal functions like alert().

What you can do is use callbacks to notify some calling code of a future event. But, the calling code will register its callback and be returned to immediately and continue executing so the calling code has to be written to handle the callback implementation.

If you're trying to do all the work inside your library, then it should not be that tough. When the caller creates a new modal, you just have to check for a pre-existing modal dialog. If one is not up you proceed as normal. If one is up, then you register a callback notification with the previous one, store the contents of the constructor, but don't actually create the new modal dialog. Then, when your callback gets called to indicate the previous modal dialog has pleted, you finish putting up the new modal.

If these modal dialogs are all of your own creation, then you need to implement pletion notification on them so that when they are closed, they can notify any listeners that they're done now. If they use an animation to close and you want to wait for the close notification until the animation is plete, then you can implement that also. If you're using CSS3 animations, then as you appear to already know, you can use the transtionEnd event to know when an animation is done or if you know the timing of the animation and you don't need to be ms precise, you can also just use a setTimeout() to know approx when the animation is plete.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742294440a4416830.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信