Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript: How to implement a queue of async functions (without libraries)
    primarykey
    data
    text
    <p>I have been searching Google and SO, but I am having a hard time wrapping my brain around this for some reason. Just to emphasize, I do NOT want to load a library like JQuery, since I don't need the majority of it. And while I <em>could</em> use an animation library, I've already built the few functions I need myself (some of them aren't even standard animations you would find in a library), so I'm not sure those would even be helpful. In fact, I could just as easily title this question "How to - Async function queue", since it isn't specific to animation.</p> <p>In my searching, I have run across the idea of Promises, but that seems like a bit more than I need for this.</p> <p>I get the general principle - you have an async function that uses setInterval / setTimeout, and given the right condition, you clear the interval (if applicable) and run a callback:</p> <pre><code>function callback() { alert('It was just a sales call... Now where were we?'); } function anim() { var i = 0, interval = setInterval( function() { // do something with the current value of i if (i === 100) { clearInterval( interval ); callback(); } i++; }, 50); } </code></pre> <p>Of course, this is better if you make it reusable, by making 'callback' an argument of anim(). However, what I truly want to be able to do is the following:</p> <pre><code>var populateDialog = new Queue([ {fn: anim, args: ['height','300px',1500]}, {fn: type, args: [input.value, outputDiv]}, {fn: highlight} ]); populateDialog.start(); </code></pre> <p>The first function would be called, and when it's complete, the next item in the queue's array would be called. The problem is, while the queue itself could easily call the functions in order, it needs to know when the current function's status is 'complete' before proceeding.</p> <p>How do I need to design any function I want to add to the queue so that it either a) publishes its state (and how do I have the queue detect that?) or b) can accept the callback from the queue? I'm guessing the former would involve some sort of custom event system, and that the latter would be easier, and look something like:</p> <pre><code>function name(callback, param1, param2, ... paramX) { // setTimeout or setInterval some stuff, then call the callback argument } </code></pre> <p>where the callback is always the first parameter, so that any number of other parameters could be tacked on. But like I said, I am having real trouble wrapping my brain around all of this, so a thorough explanation / any alternatives would be greatly appreciated!</p> <p><strong>EDIT</strong></p> <p>The queue that I came up with can be found at <a href="https://stackoverflow.com/questions/18455550/">my follow-up question</a>.</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