Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE now that OP has provided the "real" code:</strong></p> <p>The problem with putting this:</p> <pre><code>groopBox.eq(i).slideToggle(1000) </code></pre> <p>in a call to <code>setTimeout()</code> that is in a loop is that by the time all of the timeouts run the <code>for</code> loop has finished and <code>i</code> will be equal to <code>5</code>, and since there is no element with index <code>5</code> for <code>.eq(5)</code> nothing happens. You can get around that by introducing an extra "wrapper" function so that the required index ends up in a closure:</p> <pre><code>var groopBox = $("#"+value.value+" .group"); for ( var i=0 ; i&lt;groopBox.length ; i++ ) { (function(j) { setTimeout(function() { groopBox.eq(j).slideToggle(1000) }, i * 2000); })(i); } </code></pre> <p>Demo: <a href="http://jsfiddle.net/BCgkC/" rel="nofollow">http://jsfiddle.net/BCgkC/</a></p> <p>The loop's <code>i</code> is passed into the anonymous function as <code>j</code> and then the code <em>inside</em> the timeout uses <code>j</code>.</p> <p>Or you can do it rather more simply by using jQuery's <a href="http://api.jquery.com/delay/" rel="nofollow"><code>.delay()</code> method</a>, noting that <code>.delay()</code> is <em>not</em> a general replacement for <code>setTimeout()</code>, it is used only for delaying things in an element's jQuery animation queue:</p> <pre><code>var groopBox = $("#"+value.value+" .group"); for ( var i=0 ; i&lt;groopBox.length ; i++ ) { groopBox.eq(i).delay(i * 2000).slideToggle(1000); } </code></pre> <p>Demo: <a href="http://jsfiddle.net/BCgkC/1/" rel="nofollow">http://jsfiddle.net/BCgkC/1/</a></p> <p><strong>My original answer:</strong></p> <p>Note that your code had an incorrect semicolon at the end of this line:</p> <pre><code>for( var i=0 ; i&lt;5 ; i++) ; </code></pre> <p>...which means the block enclosed in <code>{}</code> isn't part of the <code>for</code> loop.</p> <p><code>setTimeout()</code> is the function used to delay execution of JavaScript code by a specified interval:</p> <pre><code>function change () { for( var i=1 ; i&lt;=5 ; i++) { setTimeout(function() { alert(5) }, i * 5000); } } </code></pre> <p>Demo: <a href="http://jsfiddle.net/kNvXh/" rel="nofollow">http://jsfiddle.net/kNvXh/</a></p> <p>JavaScript doesn't have a "blocking" pause or sleep function that prevents the next line of code from running until after the pause. The <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.setTimeout" rel="nofollow"><code>setTimeout()</code> function</a> schedules a function to run after the specified number of milliseconds, but then execution continues immediately with the statement after <code>setTimeout()</code>. So the function I've shown above runs the entire loop, queues up five timeouts five seconds apart - note how the delay specified is multiplied by the loop counter, and then ends - after each delay the queued functions are called.</p> <p>This lack of a "blocking" facility means you can't have whatever code called <code>change()</code> wait for all of the alerts before continuing. If you need to wait you need to restructure your code (perhaps with callback functions) to allow for the non-blocking nature of JS timeouts.</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.
 

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