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 8044775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:19:57+00:00 2026-06-05T05:19:57+00:00

I am working on coding for a situation where I need to construct a

  • 0

I am working on coding for a situation where I need to construct a function of nested callbacks of an unknown length. It is to create a sequenced animation queue to move an element across an unknown # of positions.

For example, output would look something like this with X ‘complete’ callbacks nested inside:

$('#element').animate(css, { complete: function () {
          $('#element').animate(css, { complete: function () {
              // more nested calls inside
          }
});

Right now I am generating these functions as a string, and then once completed, feeding it to new Function():

myFunc = new Function(generatedFuncString);

The content is trusted but this still uses eval() which has negative performance implications. I was just wondering if there is another/better way?

edit: The reason I am doing it this way is because I have a very complicated set of animations to perform and am working outside of the jQuery animation queue. If anyone has a better suggestion for how to accomplish a situation like this that would be helpful…

Imagine a baseball diamond with a runner(A) on 1st and a runner(B) on 3rd. In one animation bundle, I want to animate runner A to 3rd (stopping at 2nd in the middle, 2 advances), and runner B to HOME (1 advance).

I have to fire-off the initial advance with ‘queue: false’ so that runner A and B move to their first base at the same time (runner A to 2nd, runner B to home).

When Runner A is done moving to 2nd, I want to then move him to 3rd (hence constructing a animate() call with nested callbacks pro grammatically to ensure this sequencing is preserved).

The reason I am constructing the function via string is because I know what the inner-most callback is going to be first, and then recursively constructed 1 or more outer-callbacks from there. I couldn’t figure out a way to do this by working with functions as objects and keeping all of the references in tact.

Keep in mind this is a simple example. Imagine a situation where the bases are loaded, and I need to animate a grand slam (all 4 runners circle all bases, runner originating at home needs to make 3 stops before running back to home). Etc etc.

  • 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-06-05T05:19:58+00:00Added an answer on June 5, 2026 at 5:19 am

    Answering the question you ask in your title: You can create functions from strings via eval, new Function, and by inserting a script element with the text you want. But it all comes to the same thing: Firing up the JavaScript parser and creating the function.

    But rather than nesting, I think you want chaining. Build a list of the animations in an array, and use the animate callback to call the next animation in the array. Something like:

    var animations = [
        /* css for first animation */,
        /* css for second animation */,
        /* etc. */
    ];
    var index = 0;
    
    function runAnimation() {
        if (index < animations.length) {
            $("#element").animate(animations[index++], runAnimation);
        }
    }
    

    You’d build up the array dynamically, of course.


    gdoron points out in the comments that if all of your animations are on the same element, it can be even simpler:

    var animations = [
        /* css for first animation */,
        /* css for second animation */,
        /* etc. */
    ];
    var index = 0;
    
    for (index = 0; index < animations.length; ++index) {
        $("#element").animate(animations[index]);
    }
    

    …because when you call animate multiple times on the same element, by default the animations queue up (the queue option defaults to true; sadly the docs don’t seem to say that). My code example above doesn’t rely on the queue, and so in theory each entry in the array could be an object with a property for the selector for the elements to animate and the css to apply. But if it’s all one element, you can just use a straight loop.

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

Sidebar

Related Questions

I am working with VS 2005 and using visual basic for coding. How do
I have a problem in my coding which was working fine with an older
I'm coding a guitar hero clone game. It's working nice but in fast computers
I'm currently working at home, but it's not always an ideal environment for coding.
In VB, what coding could I use to calculate the 5th working day of
Here's the situation: I'll be coding away in VS 2010, when all of a
I've been working/coding in C#/Java for some years, so the basics etc. don't give
I have a piece of coding I am working on for an assignment for
I've been coding in J2SE and now I'm currently working on a project that
Working with a struts 1 project, I'm trying to save myself hours of coding

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.