Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6052513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:56:13+00:00 2026-05-23T07:56:13+00:00

I am having trouble figuring out the correct JQuery way of adding a series

  • 0

I am having trouble figuring out the correct JQuery way of adding a series of animation to a chain. I understand that if you know your sequence ahead of time you can just to .animate().animate(), but each animation step is dynamic in my case. I think I need to change my traditional forloop into some type of Jquery .each() loop in order to chain everything so it will run in sequence.

I am working with Jquery.svg.anim.js (jquery svg animation).

I have an array of points with an associated draw time to the next step. “G” is a grid where my coordinate are G[x][y].x => P.x and G[x][y].y => P.x

step[0] = {P:G[0][0], T:500};
step[1] = {P:G[0][1], T:2000};
step[2] = {P:G[3][1], T:100};

I then run the above through this loop:

for(s=0;s<(step.length-1);s++){
    var line1 = svg.line(step[s].P.x, step[s].P.y, step[s].P.x, step[s].P.y, {stroke: 'blue', strokeWidth: 10});
    $(line1).animate({svgX2: step[s+1].P.x, svgY2: step[s+1].P.y}, step[s].T);
}

This works to draw all of the lines to the appropriate position and over the correct length of time, but of course because its not chained properly, its doing all the animation at once. The step[] array is going to be of an arbitrary length. If I need to change around data structures I can do that at this point.

How can I properly write this in a JQuery way where I can take in an array or some collection of steps and step through each animation consecutively? I eventually want to draw an arbitrary sequence of lines to points on the SVG grid in order and fade out each line after some delay. This is eventually going to be for a “Simon” beat/timing like game where the sequence grows larger and larger and you will have to retrace the sequence properly to the beat.


Edit: The following is working to load it in order, but it seems ugly because of two loops:

var line = new Array();     
for(s=0;s<(step.length-1);s++){ 
    line[s] = svg.line(step[s].P.x, step[s].P.y, step[s].P.x, step[s].P.y, {stroke: 'blue', strokeWidth: 10});      
}

$.each(line, function(s, thisLine) { 
  $('#myQueue').queue('myQueue',function(next){
           $(thisLine).animate(
            {svgX2: step[s+1].P.x, svgY2: step[s+1].P.y}, //step[s].T
            {duration: step[s].T, queue: false, complete: next} 
        )
       }) 
});
$('#myQueue').dequeue('myQueue');

but, if I do it like this:

for(s=0;s<(step.length-1);s++){ 
    line = svg.line(step[s].P.x, step[s].P.y, step[s].P.x, step[s].P.y, {stroke: 'blue', strokeWidth: 10});     
    n=s+1;
    $('#myQueue').queue('myQueue',function(next){
           $(line).animate(
            {svgX2: step[n].P.x, svgY2: step[n].P.y},
            {duration: step[s].T, queue: false, complete: next}
           )
    })      
}
$('#myQueue').dequeue('myQueue');

It will only draw the last one; is this value vs reference? I tried wrapping the new line in an object but that did not seem to help. Update: From doing some testing the above does not work because the operations inside of the queue are not run until after they have been added, the “s” value has already been incremented all the way. That means the one with two loops is working because the iterator is restarted. I would wonder how to address this, hopefully without evals.


Final Version: The mysteries of scope in Javascript and Jquery seem to come into play. I have gotten this to work now and built the rest of my app’s first iteration. I am still not really sure why certain placements and flows work and others don’t, but for reference for anyone else who reads this:

$.each(step, function(s, thisStep) { 
    $('#myQueue').queue('myQueue',function(next){
        var thisLine = svg.line(step[s].P.x, step[s].P.y, step[s].P.x, step[s].P.y, {stroke: lineColor()/*colours[random(9)]*/, strokeWidth: 10, id: 'line'+(s+1)});
        $(thisLine).animate(
            {svgX2: step[s+1].P.x, svgY2: step[s+1].P.y}, //step[s].T
            {duration: step[s].T, queue: true, complete: next}  
        );          
    }); 
});
$('#myQueue').dequeue('myQueue');

Since I find this confusing myself, let me try to explain this for others who float by: the $.each allows you to do an iteration loop over an array. There is no for counters, so you need to add in a sentinel check at the top of the nest or do other logic if you don’t want to iterate through everything (I want to iterate only based on a specific limit so I used the following “if(!(s < Limit)){ return; }”. For each iteration, we are going to add to a named queue, as opposed to the default “fx” queue. This allows us to group a bunch of steps in the animation then run them all in once, in order (not at the same time) when you call “dequeue”. What’s important to note is the callback function named “next”, which is the second parameter for the queue function – why? Because in the collection which is the second parameter of “animate()” we have this “complete: next” This means call the function next when you are done, the callback. The other important setting there is “queue: true”, if set to false everything will happen at the same time. I am not sure if this sets up recursion per-se or just adds to the queue in order, but this setup works for me. Note, you do not have to have the queue call inside a loop. If you are wondering about the div#myQueue, its just a hidden div in the DOM to hold the named queue, and could probably be something that already exists and has a real purpose.

I hope that explanation helps someone or that someone more skilled in JFoo will add some comments below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:56:13+00:00Added an answer on May 23, 2026 at 7:56 am

    You can just use delay:

    var totalDelay=0;
    
    for(s=0;s<(step.length-1);s++){
        var line1 = svg.line(step[s].P.x, step[s].P.y, step[s].P.x, step[s].P.y, {stroke: 'blue', strokeWidth: 10});
        $(line1).delay(totalDelay).animate({svgX2: step[s+1].P.x, svgY2: step[s+1].P.y}, step[s].T);
        totalDelay+=step[s].T;
    }
    

    You could also consider using your own jquery queue. Something similar to this: Single queue for jQuery animate() elements

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble figuring out a way to conditionally cancel an asp:TextBox's OnTextChanged AutoPostBack
I having trouble figuring out the correct architecture for this kind of application: it's
I'm having some trouble figuring out the best/Djangoic way to do this. I'm creating
I'm having trouble figuring out a clean way to do this. Let's take for
I'm working on my homework and am having trouble figuring out the correct syntax
I'm having trouble figuring out the best way to have a delphi function operate
I'm having trouble figuring out how to manage memory for an instance variable that
I'm having some trouble figuring this out. I have a script that checks a
I'm having some trouble figuring out the correct syntax for mapping a many-to-many relationship
im having trouble figuring out how to bind mouseout() to my entire nav bar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.