onclick only works once

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    onclick only works once

    This simple script works first time but if you click a second time on
    the button nothing happens. I expected that the roll function would be
    executed again but it doesn't. Could someone explain why.



    javascript is at


    Thanks in advance
  • Conrad Lender

    #2
    Re: onclick only works once

    On 2008-11-20 22:00, Steve wrote:
    This simple script works first time but if you click a second time on
    the button nothing happens. I expected that the roll function would be
    executed again but it doesn't. Could someone explain why.
    function roll() {
    ^^^^
    for(var i=0; i<10; i++) {
    var roll = Math.floor(Math .random()*6+1);
    ^^^^

    Oops :-)


    - Conrad

    Comment

    • Conrad Lender

      #3
      Re: onclick only works once

      On 2008-11-20 22:10, Conrad Lender wrote:
      Oops :-)
      Err, scratch that. Sorry.

      for(var i=0; i<10; i++) {
      var roll = Math.floor(Math .random()*6+1);
      rolls.push(roll );
      ^^^^^^^^^^
      }
      for(var i=0; i<10; i++) {
      var dice="die" + i;
      document.getEle mentById(dice). innerHTML=rolls[i];
      ^^^^^

      You're pushing new values to the rolls array, but always read the same
      10 values from the start.


      - Conrad

      Comment

      • Gregor Kofler

        #4
        Re: onclick only works once

        Steve meinte:
        This simple script works first time but if you click a second time on
        the button nothing happens. I expected that the roll function would be
        executed again but it doesn't. Could someone explain why.
        >

        >
        javascript is at

        >
        Thanks in advance
        You are constantly pushing values onto the rolls array, and feeding the
        output always with the same first few entries. A quick run with a
        debugger (e.g. Firebug) would have revealed the problem.

        A

        rolls = [];

        at the beginning of the roll function should suffice. (The rest of the
        code is ..er... could be better.)

        Gregor

        Comment

        • Steve

          #5
          Re: onclick only works once

          On Nov 20, 10:17 pm, Conrad Lender <crlen...@yahoo .comwrote:
          On 2008-11-20 22:10, Conrad Lender wrote:
          >
          You're pushing new values to the rolls array, but always read the same
          10 values from the start.
          >
          Thanks Conrad.

          Comment

          • rf

            #6
            Re: onclick only works once


            "Steve" <stephen.young@ chello.atwrote in message
            news:24fa0$4925 cfd0$3eb29844$2 3511@news.chell o.at...
            This simple script works first time but if you click a second time on the
            button nothing happens. I expected that the roll function would be
            executed again but it doesn't. Could someone explain why.
            It is being executed again. You are just putting exactly the same numbers
            back there.


            Comment

            Working...