Sunday, April 29, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...