0

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

  • 1
    first off, if you want it to loop 10 times, it should be `count < 10` – mjr Jan 04 '16 at 19:20
  • What is your intent in assigning an event handler to the same element over and over again? – Pointy Jan 04 '16 at 19:21
  • Do you acknowledge that you're binding `#position1` 11 times? Move that off the loop – Adam Azad Jan 04 '16 at 19:21
  • 2
    I think this question does involve a duplicate of the linked issue, but it's got other issues too. – Pointy Jan 04 '16 at 19:22
  • The question "all it ever shows is 11" is what the dup covers. There are multiple other issues with this code too, though the OP doesn't appear to be asking about them and does not explain the objective of the code enough to address all of those either. – jfriend00 Jan 04 '16 at 19:24

0 Answers0