3

I'm using jGrowl to display messages and alerts, the datepicker I'm using is triggering the .change() function three times in short succession meaning there are three messages being displayed every time you pick a date/time, I'd like to limit this to just one.

I've tried $.jGrowl.defaults.pool = 1;but unfortunately this just causes the messages to stack, rather than actually just displaying a single message.

Here's the code I'm using, at the moment it still displays 3 messages despite the checks in place:

if($('.jGrowl-notification').length == 0) {
    jGrowl('message');
} else {
    if ( $('.jGrowl-notification').not(':visible') ) {
        jGrowl('message');
    }
}

(the first time a message is displayed, the .jGrowl-notification div is created, then when the message times out it is hidden)

Any help would be much appreciated, thank you

Nick
  • 3,745
  • 20
  • 56
  • 75
  • Can you put together a demo on jsfiddle.net? Also, which datepicker are you using? – will Jun 21 '12 at 10:46
  • 1
    Looking at the jGrowl code, the element passed into the startup has a data() attribute holding some stuff and the notifications array is in there: `$(e).data('jGrowl.instance').notifications`. worth having a look at using this to determine if notifications are already queued? – Tallmaris Jun 21 '12 at 15:00

1 Answers1

3

Could try something like (untested)

jGrowl('message',{beforeOpen: function() {
    if($('.jGrowl-notification').is(':visible')) {
       return false;
   }
 }
});

jGrowl looks at the return statement of the beforeOpen and beforeClose, returning false stops it continuing.

Another possible way is to keep a record of whether a growl is open for that input like so.

$('#yourPicker').change(function() {
   if($(this).data('hasExisting') == 0) {
    jGrowl('message',{
        open: function() {
                $('#yourPicker').data('hasExisting',1);
        },
        close: function() {
                $('#yourPicker').data('hasExisting',0);
        }
        });
   }
});

This way stores a value in the input to determine if a growl is currently open for it before it tries to queue another.

AbstractChaos
  • 4,211
  • 1
  • 19
  • 28
  • @Nick Any feedback on this awnser? – AbstractChaos Jun 27 '12 at 09:16
  • Hi, really sorry for the late response - this looks perfect! Thank you so much for taking the time to do this, I use the mono theme (http://www.webstuffshare.com/2010/01/jgrowl-theme-mono-basic/) so haven't had a chance to implement the changes yet as it could require a bit of messing about and I'm quite busy at work, but your general idea/solution is spot on, so once again, thank you! :) – Nick Jun 27 '12 at 10:32