Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to programmatically define a function from a string or some other technique without using eval()?
    primarykey
    data
    text
    <p>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.</p> <p>For example, output would look something like this with X 'complete' callbacks nested inside:</p> <pre><code>$('#element').animate(css, { complete: function () { $('#element').animate(css, { complete: function () { // more nested calls inside } }); </code></pre> <p>Right now I am generating these functions as a string, and then once completed, feeding it to new Function():</p> <pre><code>myFunc = new Function(generatedFuncString); </code></pre> <p>The content is trusted but this still uses eval() which has negative performance implications. I was just wondering if there is another/better way?</p> <p>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...</p> <p>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).</p> <p>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).</p> <p>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).</p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload