.queue()


Show or manipulate the queue of functions to be executed on the matched elements.

.queue( [queueName ] )Returns: Array

Description: Show the queue of functions to be executed on the matched elements.

Example:

Show the length of the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>queue demo</title>
<style>
div {
margin: 3px;
width: 40px;
height: 40px;
position: absolute;
left: 0px;
top: 60px;
background: green;
display: none;
}
div.newcolor {
background: blue;
}
p {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<p>The queue length is: <span></span></p>
<div></div>
<script>
var div = $( "div" );
function runIt() {
div
.show( "slow" )
.animate({ left: "+=200" }, 2000 )
.slideToggle( 1000 )
.slideToggle( "fast" )
.animate({ left: "-=200" }, 1500 )
.hide( "slow" )
.show( 1200 )
.slideUp( "normal", runIt );
}
function showIt() {
var n = div.queue( "fx" );
$( "span" ).text( n.length );
setTimeout( showIt, 100 );
}
runIt();
showIt();
</script>
</body>
</html>

Demo:

.queue( [queueName ], newQueue )Returns: jQuery

Description: Manipulate the queue of functions to be executed, once for each matched element.

  • version added: 1.2.queue( [queueName ], newQueue )

    • queueName
      Type: String
      A string containing the name of the queue. Defaults to fx, the standard effects queue.
    • newQueue
      Type: Array
      An array of functions to replace the current queue contents.
  • version added: 1.2.queue( [queueName ], callback )

    • queueName
      Type: String
      A string containing the name of the queue. Defaults to fx, the standard effects queue.
    • callback
      Type: Function( Function next() )
      The new function to add to the queue, with a function to call that will dequeue the next item.

Every element can have one to many 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 typical example of this is calling multiple animation methods on an element. For example:

1
$( "#foo" ).slideUp().fadeIn();

When this statement is executed, the element begins its sliding animation immediately, but the fading transition is placed on the fx queue to be called only once the sliding transition is complete.

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

This feature is similar to providing a callback function with an animation method, but does not require the callback to be given at the time the animation is performed.

1
2
3
4
5
$( "#foo" ).slideUp();
$( "#foo" ).queue(function() {
alert( "Animation complete." );
$( this ).dequeue();
});

This is equivalent to:

1
2
3
$( "#foo" ).slideUp(function() {
alert( "Animation complete." );
});

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

As of jQuery 1.4, the function that's called is passed another function as the first argument. When called, this automatically dequeues the next item and keeps the queue moving. We use it as follows:

1
2
3
4
$( "#test" ).queue(function( next ) {
// Do some stuff...
next();
});

Examples:

Queue a custom function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>queue demo</title>
<style>
div {
margin: 3px;
width: 40px;
height: 40px;
position: absolute;
left: 0px;
top: 30px;
background: green;
display: none;
}
div.newcolor {
background: blue;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
Click here...
<div></div>
<script>
$( document.body ).on( "click", function() {
$( "div" )
.show( "slow" )
.animate( { left: "+=200" }, 2000 )
.queue(function() {
$( this ).addClass( "newcolor" ).dequeue();
})
.animate( { left: "-=200" }, 500 )
.queue(function() {
$( this ).removeClass( "newcolor" ).dequeue();
} )
.slideUp();
} );
</script>
</body>
</html>

Demo:

Set a queue array to delete the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>queue demo</title>
<style>
div {
margin: 3px;
width: 40px;
height: 40px;
position: absolute;
left: 0px;
top: 30px;
background: green;
display: none;
}
div.newcolor {
background: blue;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<button id="start">Start</button>
<button id="stop">Stop</button>
<div></div>
<script>
$( "#start" ).on( "click", function() {
$( "div" )
.show( "slow" )
.animate({ left: "+=200" }, 5000 )
.queue(function() {
$( this ).addClass( "newcolor" ).dequeue();
})
.animate({ left: '-=200' }, 1500 )
.queue(function() {
$( this ).removeClass( "newcolor" ).dequeue();
})
.slideUp();
});
$( "#stop" ).on( "click", function() {
$( "div" )
.queue( "fx", [] )
.stop();
});
</script>
</body>
</html>

Demo: