Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would first add a callback to <code>asyncMeth</code> that is called whenever the function has finished doing whatever it is doing. Otherwise there is no way to know if <code>asyncMeth</code> has finished its work. In your example what you have isn't the same as what you want! You want <code>ads[i]</code> to process 3 seconds after <code>ads[i - 1]</code> has <em>finished</em>. Instead, what you have is code to start processing <code>ads[i]</code> 3 seconds after <code>ads[i - 1]</code> has <em>started processing</em>. With a callback to <code>asyncMeth</code>, you can then do this:</p> <pre><code>(function work(i) { if(i &lt; ads.length) { asyncMeth(ads[i], function() { work(++i); }); } }(0); </code></pre> <p>Here you have a self-invoked function that initially accepts a parameter value of <code>0</code>. This value is assigned to the parameter <code>i</code>. If <code>i</code> is lesser than <code>ads.length</code>, it means that we still have items to process. So it calls <code>asyncMeth</code> with <code>ads[i]</code> as a parameter. </p> <p>But then we also provide a callback. In this callback we tell the callback to call <code>work</code> with the incremented the value of <code>i</code>. This means that <code>work</code> will start processing the next item. </p> <p>So now you can asynchronously-process each item one after the other until <code>i</code> becomes equal to <code>ads.length</code>, at which point you have processed all the items.</p> <p><strong>EDIT</strong></p> <p>I noticed that you mentioned that you want a 3-second delay. For that you can do this:</p> <pre><code>(function work(i) { if(i &lt; ads.length) { asyncMeth(ads[i], function() { setTimeout(function() { work(++i); }, 3000); }); } }(0); </code></pre> <p>The only difference here is that the next iteration happens 3 seconds after the last one has completed.</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.
    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