Change value of DOM element by Javascript loop : <p>Following code logs a value 0 to 100 in console with 1000ms delay in between. But it does not update DOM element div.innerHTML as intended. How to change DOM element value by JS loop with delay. Looks like setTimeout does not work in loop.</p>

<p><strong>HTML file</strong></p>

<pre><code>&lt;div id="output"&gt;&lt;/div&gt;


&lt;script&gt;
var obj=document.getElementById("output");

for(var i=0;i&lt;100;i++){
obj.innerHTML=i.toString();
(function(i){
    setTimeout(function(){
      console.log(i);
  }, 1000 * i)
 })(i);
}
&lt;/script&gt;
</code></pre>
