Sunday, 29 April 2018

javascript - Why does my for loop go straight to the end?

I am trying to learn javascript basics through an online course. I have a set of functions that work and I want to run them through a for loop 10 times. Here is the code of my for loop:



for (var count=0; count < 11; count++){
document.getElementById("position1").onclick=function() {
this.style.display="none";
clicked=Date.now();
reaction=(clicked-created)/1000;
document.getElementById("timebox").innerHTML="Time to click: "+reaction+" seconds "+count;
makebox();
}
}


For some reason, the loop starts with the variable count at 11 and infinately runs the code instead of stopping after 10 times. Can anybody give me an idea why it is doing this? I have added the count variable to the output just to see it and all it ever shows is 11.Thank you for any help that you can offer as I am stumped.



David

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...