0

I'm trying to generate 3 divs in a for loop and insert the datetime in each iteration . The problem I'm running into is while the function generates the three div's correctly it appends the same time to all 3 div's leaving me (JavaScript newbie) to believe the Date() function is only being executed once . If any we could explain to me what is going on I would greatly appreciate it. Ideally I would like to replace the Date function with graph's and have a graph load in each div.

function gengraphs () {
  for (i=0; i < 3; ++i) {
    var divTag = document.createElement("div");
    divTag.style.width ="1000px";
    divTag.style.height ="600px";
    divTag.id = "graph"+i;
    document.body.appendChild(divTag);
    divTag.innerHTML = Date();

    // divTag.appendChild(getGraph(divTag));   
    // divTag.innerHTML = getGraph(divTag);
  }
}
Vicky Leong
  • 1,208
  • 3
  • 12
  • 30

2 Answers2

0

The server is executing the script fast enough (in milliseconds) that the date() returned is not visibly different. Try something to delay or use the increment variable i

Kirk Powell
  • 908
  • 9
  • 14
0

The loop is being executed so quickly that you won't see a difference in time. Try calling the contents of your loop with a significant delay (e.g. 1s) and put it into a function as shown here: JavaScript sleep/wait before continuing

Community
  • 1
  • 1
SCBuergel
  • 1,613
  • 16
  • 26
  • Also when I uncomment a function that displays a graph in the place of calling Date() it only displays one graph instead of displaying three separate graphs each in its own div. – user3666137 Jun 02 '15 at 21:10