Home  >  Article  >  Web Front-end  >  Detailed explanation of jQuery queue control methods queue()/dequeue()/clearQueue()

Detailed explanation of jQuery queue control methods queue()/dequeue()/clearQueue()

巴扎黑
巴扎黑Original
2017-06-20 15:08:521232browse

jQueryIn the core, there is a set of queue control methods. This set of methods consists of three methods: queue()/dequeue()/clearQueue(). The control of functions executed sequentially can be said to be concise and easy. It is mainly used in the animate () method, ajax and other events that need to be executed in chronological order.

Let’s first explain this set of methods Their respective meanings.
queue(name,[callback]): When only one parameter is passed in, it returns and points to the queue of the first matching element (will be an array of functions, queue name The default is fx); when two parameters are passed in, the first parameter still defaults to the queue name of fx, and the second parameter is divided into two situations. When the second parameter is a function, it will be in A function is finally added to the queue of matching elements. When the second parameter is a function array, it will replace the queue of matching elements with a new queue (function array). Maybe, this is a bit confusing to understand. Later, You will see the DEMO later.

dequeue(name): This is easy to understand, it is to remove a queue function from the front of the queue and execute it.
clearQueue ([queueName]):This is a new method added in 1.4. Clear all queues that have not yet been executed on the object. The parameter is optional and the default is fx. However, I personally feel that this method is not very useful, so use The queue() method can implement the clearQueue method by passing in the second parameter of the two parameters.
Now, we want to achieve such an effect. There are numbered squares marked from 1 to 7, and these seven squares are required from left to Click here to view the DEMO


I won’t post the css and html parts, they are included in the DEMO demo. If you click Conventionally, you may need to use the following jQ code to achieve it:


$('.one').delay(500).animate({top:'+=270px'},500,function(){
		$('.two').delay(500).animate({top:'+=270px'},500,function(){
			$('.three').delay(500).animate({top:'+=270px'},500,function(){
				$('.four').delay(500).animate({top:'+=270px'},500,function(){
					$('.five').delay(500).animate({top:'+=270px'},500,function(){
						$('.six').delay(500).animate({top:'+=270px'},500,function(){
							$('.seven').animate({top:'+=270px'},500,function(){
								alert('按序落体运动结束! Yeah!')
							});
						});
					});
				});
			});
		});
	});

Well, yes, the effect is perfectly presented, but this kind of dizzy code, Can you bear it? Even if you can bear it, what if you want to change a certain execution order at this time, for example, you want 5 to fall before starting to fall 3, or add eight new blocks from 8 to 15, what should you do? Re- Do you want to write it? Are you careful about the changes in it? Obviously, we need another concise and convenient method to achieve this effect, that is, the jQuery queue control method. Please see the following code:


var _slideFun=[
	function(){$('.one').delay(500).animate({top:'+=270px'},500,_takeOne);},
	function(){$('.two').delay(300).animate({top:'+=270px'},500,_takeOne);},
	function(){$('.three').delay(300).animate({top:'+=270px'},500,_takeOne);},
	function(){$('.four').delay(300).animate({top:'+=270px'},500,_takeOne);},
	function(){$('.five').delay(300).animate({top:'+=270px'},500,_takeOne);},
	function(){$('.six').delay(300).animate({top:'+=270px'},500,_takeOne);},
	function(){$('.seven').delay(300).animate({top:'+=270px'},500,function(){
		alert('按序落体运动结束! Yeah!');
	});}
];
$('#demo').queue('slideList',_slideFun);
var _takeOne=function(){
	$('#demo').dequeue('slideList');
};
_takeOne();

In this way, does it seem much simpler? How to implement it?

1. Create a new array and put the
animation functions into it in sequence (change the order in this way and add a new Is animation much more convenient?); 2. Use queue to add this set of animation function arrays to the slideList queue;
3. Use dequeue to take out the first function in the slideList queue and execute it;
4. Initial execution of the first function.
There are also detailed explanations in the DEMO demonstration
Comments. If you still don’t understand the above explanation, please look at the source code. As for the clearQueue() method, just Not much to say, the stop button in the demonstration calls the clearQueue() method. Of course, you can also use the queue() method to directly replace the current function queue with an [] empty array implementation (I personally recommend empty array replacement., which is more intuitive) ).

The above is the detailed content of Detailed explanation of jQuery queue control methods queue()/dequeue()/clearQueue(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn