Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I chain or queue custom functions using JQuery?

Tags:

I have multiple functions the do different animations to different parts of the HTML. I would like to chain or queue these functions so they will run the animations sequentially and not at the same time.

I am trying to automate multiple events in sequence to look like a user has been clicking on different buttons or links.

I could probably do this using callback functions but then I would have to pull all of the animations from the different functions and regroup in the right pattern.

Does the jquery "queue" help? I couldn't understand the documentation for the queue.

Example, JQuery:

 function One() {         $('div#animateTest1').animate({ left: '+=200' }, 2000);     }     function Two() {         $('div#animateTest2').animate({ width: '+=200' }, 2000);     }  // Call these functions sequentially so that the animations // in One() run b/f the animations in Two()     One();     Two(); 

HTML:

    <div id="animatetest" style="width:50px;height:50px;background-color:Aqua;position:absolute;"></div>     <div id="animatetest2" style="width:50px;height:50px;background-color:red;position:absolute;top:100px;"></div> 

Thanks.

EDIT: I tried it with timers but I thought there is a better way to do it.

EDIT #2:

Let me be more specific. I have multiple functions bound to click & hover events on different elements of the page. Normally these functions have nothing to do with each other ( they don't reference each other). I would like to simulate a user going through these events without changing the code of my existing functions.

like image 944
orandov Avatar asked Jul 07 '09 22:07

orandov


People also ask

How to queue function jQuery?

The queue() method shows the queue of functions to be executed on the selected elements. A queue is one or more function(s) waiting to run. The queue() method can be used together with the dequeue() method.

How to create queue functionality for animation in jQuery?

jQuery animate() - Uses Queue Functionality By default, jQuery comes with queue functionality for animations. This means that if you write multiple animate() calls after each other, jQuery creates an "internal" queue with these method calls. Then it runs the animate calls ONE by ONE.


1 Answers

The jQuery Queue code is not as well documented as it could be, but the basic idea is this:

$("#some_element").queue("namedQueue", function() {   console.log("First");   var self = this;   setTimeout(function() {     $(self).dequeue("namedQueue");   }, 1000); });  $("#some_element").queue("namedQueue", function() {   console.log("Second");   var self = this;   setTimeout(function() {     $(self).dequeue("namedQueue");   }, 1000); });  $("#some_element").queue("namedQueue", function() {   console.log("Third"); });  $("#some_element").dequeue("namedQueue"); 

If you run this code, you will see "First" in the console, a pause of 1s, see "Second" in the console, another pause of 1s, and finally see "Third" in the console.

The basic idea is that you can have any number of named queues bound to an element. You remove an element from the queue by calling dequeue. You can do this yourself manually as many times as you want, but if you want the queue to run automatically, you can simply call dequeue inside the queued up function.

Animations in jQuery all run inside a queue called fx. When you animate an element, it automatically adds the new animation on the queue and calls dequeue if no animations are currently running. You can insert your own callbacks onto the fx queue if you wish; you will just need to call dequeue manually at the end (as with any other queue use).

like image 154
Yehuda Katz Avatar answered Sep 29 '22 19:09

Yehuda Katz