views:

226

answers:

3

Hello,

I've got a short function that should show messages on a website.

function showHint() { 
    $('#notify').html('message text').show('slide', {direction: 'right'}, 500);
}

And there is another function that hides the messages.

function hideHint() { 
    $('#notify').hide('slide', {direction: 'right'}, 500);
}

The Problem is that if I call this function more than one times it tries to show all messages at the same time and everything breaks. I want to call the function twice and then it should queue the animations and show one message after another. The function should be called more than one times at the same time but shown one after another. The next message should be shown when the firs hides.

How could I solve the Problem? Would be nice!

+1  A: 

Here's a mini custom plugin that I've used in the past that chains a bunch of animations one after another.

// Good for serializing animations
$.fn.chain = function(fn) {
  var elements = this;
  var i = 0;
  function nextAction() {
    if (elements.eq(i)) fn.apply(elements.eq(i), [nextAction]);
    i++;
  }
  nextAction();
};

You might call it like so (Here's an example of it in use):

$(document).ready(function() {
  $('li').chain(function(nextChain) { this.slideToggle("fast", nextChain); });
});

The function you pass to chain passes another function that you must call when you're down with one cycle. In the example above, we just pass the nextChain function as the callback to the slideToggle.

Ryan McGeary
That's a nice solution, but it doesn't help me out of my problem. The Problem is that the text of the message is changed before the first message is hidden.
dominik
dominik, You don't have to chain everything after document load. You can chain everything after the text of the message is changed.
Ryan McGeary
A: 

Your showhint function could just start by hiding the notification and when that is complete the callback would be what is the existing showhint function, that would change the text and show it. Code shouldn't be difficult given what you've already done.

jarrett
A: 

can you not just use a notification plugin? here are two (one, two) that are pretty spiffy.

contagious
Yea, pnotify is quite good. http://plugins.jquery.com/project/pnotify
dominik