Question

As explained in another question, If the queue being queued is fx and the queue is not already in progress, dequeue is called..

That is great that the default queue is smart enough to auto start. But how can I replicate the same functionality, auto starting when the queue is empty, for my custom queue?

My problem is I am receiving Pusher calls (ajax calls that come to the page) that activate the code below to add a new name to #tickerlist. The problem is, to get the queue started, I have to add .dequeue outside the queue, which then forces tickerlistqueue to start the next item in the queue. When you've got several Pusher calls coming in at once, this effectively renders the queue useless - the jobs just run over each other because that's exactly what I'm telling it to do.

Essentially what I need is to only run the .dequeue line when the queue is empty. Otherwise, don't run that line, the queue will take care of itself.

var tickerlist = $('#ticker-list');

tickerlist.queue('tickerlistqueue', function(next) {
    var name = 'John';
    $(name).hide().prependTo('#ticker-list').slideDown(700, function () {
        $('#ticker-list>div:last').remove();
        next();
    })
});

tickerlist.dequeue('tickerlistqueue'); //This is the only way to start the queue
Was it helpful?

Solution

Duh; my last paragraph answered my own question. jQuery shows you how to get the size of the queue in it's first example.

If queue length is 1 or less, start the queue. Otherwise, don't do anything. It's 1, not 0, because you are calling this after you queue the latest name.

Particular to my example:

if (tickerlist.queue('tickerlistqueue').length <= 1) {
    tickerlist.dequeue('tickerlistqueue');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top