Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first form that you list works, since it will evaluate a string at the end of <code>delay</code>. Using <code>eval()</code> is generally not a good idea, so you should avoid this.</p> <p>The second method doesn't work, since you immediately execute a function object with <strong><a href="http://docstore.mik.ua/orelly/webprog/jscript/ch05_10.htm" rel="noreferrer">the function call operator <code>()</code></a></strong>. What ends up happening is that <code>playNote</code> is executed immediately if you use the form <code>playNote(...)</code>, so nothing will happen at the end of the delay.</p> <p>Instead, you have to pass an anonymous function to setTimeout, so the correct form is:</p> <pre><code>setTimeout(function() { playNote(currentaudio.id,noteTime) }, delay); </code></pre> <p>Note that you are passing <code>setTimeout</code> an entire function expression, so it will hold on to the anonymous function and only execute it at the end of the delay.</p> <p>You can also pass <code>setTimeout</code> a reference, since a reference isn't executed immediately, but then you can't pass arguments:</p> <pre><code>setTimeout(playNote, delay); </code></pre> <hr> <p><strong>Note:</strong></p> <p>For repeated events you can use <strong><a href="https://developer.mozilla.org/en/DOM/window.setInterval" rel="noreferrer"><code>setInterval()</code></a></strong> and you can set <code>setInterval()</code> to a variable and use the variable to stop the interval with <strong><a href="https://developer.mozilla.org/en/DOM/window.clearInterval" rel="noreferrer"><code>clearInterval()</code></a></strong>.</p> <p>You say you use <code>setTimeout()</code> in a <code>for</code> loop. In many situations, it is better to use <code>setTimeout()</code> in a recursive function. This is because in a <code>for</code> loop, the variables used in the <code>setTimeout()</code> will not be the variables as they were when <code>setTimeout()</code> began, but the variables as they are after the delay when the function is fired.</p> <p>Just use a recursive function to sidestep this entire problem.</p> <h2>Using recursion to deal with variable delay times:</h2> <pre><code> // Set original delay var delay = 500; // Call the function for the first time, to begin the recursion. playNote(xxx, yyy); // The recursive function function playNote(theId, theTime) { // Do whatever has to be done // ... // Have the function call itself again after a delay, if necessary // you can modify the arguments that you use here. As an // example I add 20 to theTime each time. You can also modify // the delay. I add 1/2 a second to the delay each time as an example. // You can use a condition to continue or stop the recursion delay += 500; if (condition) { setTimeout(function() { playNote(theID, theTime + 20) }, delay); } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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