Runtime error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alistair@nts-graphics.co.uk

    Runtime error

    Hi all,

    The following JS function gives a runtime error - 'Error myButton is
    undefined' when the function tries to call itself.

    The function basically tries to [simply] animate a buttons text to show
    the user that something is happening . . .

    function buttonText(myBu tton) {
    var A='/', B='-', C='\\', D='|';
    var Text=eval(myBut ton+'.value');
    switch (Text) {
    case '|':
    eval(myButton+' .value=A');
    break;
    case '/':
    eval(myButton+' .value=B');
    break;
    case '-':
    eval(myButton+' .value=C');
    break;
    default:
    eval(myButton+' .value=D');
    break;
    }
    setTimeout('but tonText(myButto n)',150);
    }

  • Erwin Moller

    #2
    Re: Runtime error

    alistair@nts-graphics.co.uk wrote:
    [color=blue]
    > Hi all,
    >
    > The following JS function gives a runtime error - 'Error myButton is
    > undefined' when the function tries to call itself.
    >
    > The function basically tries to [simply] animate a buttons text to show
    > the user that something is happening . . .
    >
    > function buttonText(myBu tton) {
    > var A='/', B='-', C='\\', D='|';
    > var Text=eval(myBut ton+'.value');
    > switch (Text) {
    > case '|':
    > eval(myButton+' .value=A');
    > break;
    > case '/':
    > eval(myButton+' .value=B');
    > break;
    > case '-':
    > eval(myButton+' .value=C');
    > break;
    > default:
    > eval(myButton+' .value=D');
    > break;
    > }
    > setTimeout('but tonText(myButto n)',150);
    > }[/color]

    Hi,

    I think in your call:
    setTimeout('but tonText(myButto n)',150);
    button doesn't contain the original value (a reference to a button??).

    You better rewrite your code and pass a number of string to the function.

    setTimeout('but tonText(1)',150 );

    Of course you'll have to change the function too, so it understands 1.
    when receiving 1, set 2 in the timeout-call.
    etc.

    Regards,
    Erwin Moller

    Comment

    • RobB

      #3
      Re: Runtime error

      function buttonText(butt on_id)
      {
      var b = document.getEle mentById(button _id);
      if (b)
      {
      switch (b.value)
      {
      case '|' :
      b.value = '/';
      break;
      case '/' :
      b.value = '-';
      break;
      case '-' :
      b.value = '\\';
      break;
      default :
      b.value = '|';
      }
      }
      }

      setTimeout('but tonText("button 1")', 150);

      <input id="button1"... ../>

      Comment

      • RobB

        #4
        Re: Runtime error

        OK, had a little fun here. See if there's anything you can use. =:o

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        <title>untitled </title>
        <style type="text/css">

        ..button {
        width: 60px;
        font: bold 14px tahoma;
        color: #060;
        background: #efe0b8;
        }

        </style>
        <script type="text/javascript">

        var B =
        {
        timer : null ,
        active : false ,
        curr : null ,
        nglyph : 0 ,
        glyphs : ['|','/','--','\\']
        }

        function butspin(but)
        {
        init();
        if (!B.active && but != B.curr)
        {
        but.origValue = but.value;
        B.active = true;
        B.curr = but;
        B.timer = setInterval(ani mate, 100);
        }
        else B.curr = null;
        }

        function animate()
        {
        B.curr.value = B.glyphs[B.nglyph++ % 4];
        }

        function init()
        {
        if (B.timer)
        {
        clearInterval(B .timer);
        B.active = false;
        }
        if (B.curr && B.curr.origValu e)
        B.curr.value = B.curr.origValu e;
        B.nglyph = 0;
        }

        onunload = init;

        </script>
        </head>
        <body style="margin:1 00px;">
        <hr />
        <form>
        <input id="button1"
        class="button"
        type="button"
        value="foo"
        onfocus="this.b lur()"
        onclick="butspi n(this)" />
        <input id="button2"
        class="button"
        type="button"
        value="feh"
        onfocus="this.b lur()"
        onclick="butspi n(this)" />
        <input id="button3"
        class="button"
        type="button"
        value="hah"
        onfocus="this.b lur()"
        onclick="butspi n(this)" />
        <input
        class="button"
        type="button"
        value="stop"
        style="color:#6 00;margin-left:5px;"
        onfocus="this.b lur()"
        onclick="init() " />
        </form>
        <hr />
        </body>
        </html>

        Comment

        • alistair@nts-graphics.co.uk

          #5
          Re: Runtime error

          Thanks guys, the last solution did the trick :-)

          Comment

          Working...