Runtime error

A

alistair

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);
}
 
E

Erwin Moller

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
 
R

RobB

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"...../>
 
R

RobB

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top