Runtime error

Discussion in 'Javascript' started by alistair@nts-graphics.co.uk, Feb 16, 2005.

  1. Guest

    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(myButton) {
    var A='/', B='-', C='\\', D='|';
    var Text=eval(myButton+'.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('buttonText(myButton)',150);
    }
     
    , Feb 16, 2005
    #1
    1. Advertising

  2. Erwin Moller Guest

    wrote:

    > 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(myButton) {
    > var A='/', B='-', C='\\', D='|';
    > var Text=eval(myButton+'.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('buttonText(myButton)',150);
    > }


    Hi,

    I think in your call:
    setTimeout('buttonText(myButton)',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('buttonText(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
     
    Erwin Moller, Feb 16, 2005
    #2
    1. Advertising

  3. RobB Guest

    function buttonText(button_id)
    {
    var b = document.getElementById(button_id);
    if (b)
    {
    switch (b.value)
    {
    case '|' :
    b.value = '/';
    break;
    case '/' :
    b.value = '-';
    break;
    case '-' :
    b.value = '\\';
    break;
    default :
    b.value = '|';
    }
    }
    }

    setTimeout('buttonText("button1")', 150);

    <input id="button1"...../>
     
    RobB, Feb 16, 2005
    #3
  4. RobB Guest

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

    <!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(animate, 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.origValue)
    B.curr.value = B.curr.origValue;
    B.nglyph = 0;
    }

    onunload = init;

    </script>
    </head>
    <body style="margin:100px;">
    <hr />
    <form>
    <input id="button1"
    class="button"
    type="button"
    value="foo"
    onfocus="this.blur()"
    onclick="butspin(this)" />
    <input id="button2"
    class="button"
    type="button"
    value="feh"
    onfocus="this.blur()"
    onclick="butspin(this)" />
    <input id="button3"
    class="button"
    type="button"
    value="hah"
    onfocus="this.blur()"
    onclick="butspin(this)" />
    <input
    class="button"
    type="button"
    value="stop"
    style="color:#600;margin-left:5px;"
    onfocus="this.blur()"
    onclick="init()" />
    </form>
    <hr />
    </body>
    </html>
     
    RobB, Feb 16, 2005
    #4
  5. Guest

    Thanks guys, the last solution did the trick :)
     
    , Feb 17, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Polo Lee
    Replies:
    0
    Views:
    2,734
    Polo Lee
    Jul 7, 2003
  2. Yongsub Eric Shin
    Replies:
    7
    Views:
    865
    Martin
    Jan 14, 2004
  3. Kevin Farless
    Replies:
    13
    Views:
    15,235
    chesterchass
    Oct 3, 2007
  4. Krunom Ancini
    Replies:
    2
    Views:
    5,169
  5. Hal Vaughan
    Replies:
    11
    Views:
    1,130
    Gordon Beaton
    May 22, 2006
Loading...

Share This Page