Delayed Javascript Show

A simple example:

for (var i = 0; i < 10; ++i) {
  console.log(i); // <--- should be show with delay in 300ms 
}

The simple use of setTimeout, of course, does not work ... I assume you need to use closure.

+3
source share
3 answers

Must be performed:

for (var i = 0; i < 10; ++i) {
  (function(i) {
     setTimeout(function(){console.log(i);}, i*300);
  })(i);
}
+4
source

This is a simple question about writing a recursive function:

function display(i)
{
  if (i == 10) return;    
  setTimeout(function(){ console.log(i); display(i+1); }, 300);
}
+6
source

You can use setInterval, for example:

var i = 0;
var id = setInterval(function(){
    if (i == 9) clearInterval(id);
    console.log(i);
    i++;
}, 300);

An example is here http://jsfiddle.net/MLWgG/2/

+3
source

Source: https://habr.com/ru/post/1755308/


All Articles