jQuery.queue()

Show the queue of functions to be executed on the matched element. Manipulate the queue of functions to be executed on the matched element.

jQuery.queue(element, queueName)🡢 Array

element ElementA DOM element to inspect for an attached queue.
queueName StringA string containing the name of the queue. Defaults to fx, the standard effects queue.

Note: This is a low-level method, you should probably use .queue() instead.

Show the length of the queue.

JS
<button id="show">Show Length of Queue</button>
<span></span>
<div></div>
CSS
div {
  margin: 3px;
  width: 40px;
  height: 40px;
  position: absolute;
  left: 0px;
  top: 30px;
  background: green;
  display: none;
}
div.newcolor {
  background: blue;
}
span {
  color: red;
}
HTML
$("#show").click(function () {
  var n = jQuery.queue($("div")[0], "fx");
  $("span").text("Queue length is: " + n.length);
});

function runIt() {
  $("div")
    .show("slow")
    .animate(
      {
        left: "+=200",
      },
      2000
    )
    .slideToggle(1000)
    .slideToggle("fast")
    .animate(
      {
        left: "-=200",
      },
      1500
    )
    .hide("slow")
    .show(1200)
    .slideUp("normal", runIt);
}

runIt();
DEMO

jQuery.queue(element, queueName, newQueue)🡢 Array

element ElementA DOM element where the array of queued functions is attached.
queueName StringA string containing the name of the queue. Defaults to fx, the standard effects queue.
newQueue ArrayAn array of functions to replace the current queue contents.

jQuery.queue(element, queueName, callback)🡢 Array

element ElementA DOM element on which to add a queued function.
queueName StringA string containing the name of the queue. Defaults to fx, the standard effects queue.
callback FunctionThe new function to add to the queue.

Note: This is a low-level method, you should probably use .queue() instead.

Every element can have one or more queues of functions attached to it by jQuery. In most applications, only one queue (called fx) is used. Queues allow a sequence of actions to be called on an element asynchronously, without halting program execution.

The jQuery.queue() method allows us to directly manipulate this queue of functions. Calling jQuery.queue() with a callback is particularly useful; it allows us to place a new function at the end of the queue.

Note that when adding a function with jQuery.queue(), we should ensure that jQuery.dequeue() is eventually called so that the next function in line executes.

Queue a custom function.

JS
Click here...
<div></div>
CSS
div {
  margin: 3px;
  width: 40px;
  height: 40px;
  position: absolute;
  left: 0px;
  top: 30px;
  background: green;
  display: none;
}
div.newcolor {
  background: blue;
}
HTML
$(document.body).click(function () {
  var divs = $("div").show("slow").animate({ left: "+=200" }, 2000);
  jQuery.queue(divs[0], "fx", function () {
    $(this).addClass("newcolor");
    jQuery.dequeue(this);
  });
  divs.animate({ left: "-=200" }, 500);
  jQuery.queue(divs[0], "fx", function () {
    $(this).removeClass("newcolor");
    jQuery.dequeue(this);
  });
  divs.slideUp();
});
DEMO

Set a queue array to delete the queue.

JS
<button id="start">Start</button>
<button id="stop">Stop</button>
<div></div>
CSS
div {
  margin: 3px;
  width: 40px;
  height: 40px;
  position: absolute;
  left: 0px;
  top: 30px;
  background: green;
  display: none;
}
div.newcolor {
  background: blue;
}
HTML
$("#start").click(function () {
  var divs = $("div").show("slow").animate({ left: "+=200" }, 5000);
  jQuery.queue(divs[0], "fx", function () {
    $(this).addClass("newcolor");
    jQuery.dequeue(this);
  });
  divs.animate({ left: "-=200" }, 1500);
  jQuery.queue(divs[0], "fx", function () {
    $(this).removeClass("newcolor");
    jQuery.dequeue(this);
  });
  divs.slideUp();
});
$("#stop").click(function () {
  jQuery.queue($("div")[0], "fx", []);
  $("div").stop();
});
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)