Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer the question, to get something like a sleep function you could just write somehting like this as a helper function</p> <pre><code>function sleep(dur) { var d = new Date().getTime() + dur; while(new Date().getTime() &lt;= d ) { //Do nothing } } console.log(new Date().getTime()) sleep(1000) console.log(new Date().getTime()) </code></pre> <p>Then you <em>could</em> call the <code>sleep</code> function after every iteration like</p> <pre><code>function abc(){ for(i=1;i&lt;=10;i++){ document.write(i+"&lt;br&gt;"); sleep(1000); } } </code></pre> <p>But Note that <code>sleep</code> will freeze your browser in this time and you don't really wan't this kind of behaviour when you just want to periodiccally do sth</p> <p><code>window.setInterval</code> would be what you want in such cases</p> <pre><code> function abcd(i){ document.write(i + "&lt;br&gt;") } function repeatedTimeout(func,times,duration) { var args = Array.prototype.slice.call(arguments).splice(3); var i = 0; args.push(i) var wrap = function () { if(args[args.length - 1] &gt;= times) window.clearInterval(wrap) else { func.apply(this,args) args[args.length - 1]++ } } window.setInterval(wrap,duration) } repeatedTimeout(abcd,10,1000) </code></pre> <p>Which would call it 10 times every 1000 milliseconds, whithout freezing the Browers</p> <p>Heres the <a href="http://jsbin.com/iyaser/1/edit" rel="nofollow">JSBin</a></p> <h2>Update</h2> <p>If it really has to be a for loop, you could do something like this, regardless of the sense it makes to me </p> <pre><code>for (var i = 0; i &lt;= 10 ; i++) { window.setTimeout( (function (i){ return function() { document.write(i + "&lt;br&gt;") } })(i),i * 1000) } </code></pre> <p>In this case heres another <a href="http://jsbin.com/okusez/1/edit" rel="nofollow">JSBin</a></p> <p>This would call <code>window.setTimeout</code> in a <code>for</code> loop and a multiple of the timeout with <code>i</code> as the timeout, this would work, but i'd rather suggest using <code>setInterval</code> like you already did in the Fiddle you posted in the comment</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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