...

View Full Version : setTimeout - newbie question



dudeshouse
11-08-2006, 05:48 PM
Hi, I've had a search and couldn't find the answer, so apologies if I'm repeating an already answered question...

I'm trying to get something really simple working, code is:

<html>
<head>
<script language="JavaScript">
function switchswap(swno) {
document.switchform.switchtext.value="You pushed switch no. " + swno;
setTimeout(document.switchform.switchtext2.value="Switch no. " + swno + " was swapped back",1000);
}

</script>

</head>
<body>
<a href="javascript:switchswap(1)">No. 1</a>
<a href="javascript:switchswap(2)">No. 2</a>

<form name="switchform">
<input type="text" id="switchtext" value="0" size="40">
<input type="text" id="switchtext2" value="0" size="40">
</form>
</body>
</html>

All I want to happen is for an event to be fired, then another to fire at a preset time. I've put the text boxes in to test for now. All that happens is that both events happen at the same time.

I've tried separating the functions and calling the 2nd one from within the first and the same thing happens. It doesn't seem to make any difference if I call a function from within the setTimeout or whether I put the code in.

Any help would be greatly appreciated.

Thanks,

Glenn

_Aerospace_Eng_
11-08-2006, 06:12 PM
You can use an anonymous function in the setTimeout

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using an anonymous function in setTimeout</title>
<script type="text/javascript">
function switchswap(swno)
{
document.switchform.switchtext.value="You pushed switch no. " + swno;
setTimeout
(
function()
{
document.switchform.switchtext2.value="Switch no. " + swno + " was swapped back"
}
,1000
);
return false;
}
</script>

</head>
<body>
<a href="#" onclick="return switchswap(1);">No. 1</a>
<a href="#" onclick="return switchswap(2);">No. 2</a>
<form name="switchform">
<input type="text" id="switchtext" value="0" size="40">
<input type="text" id="switchtext2" value="0" size="40">
</form>
</body>
</html>
http://phrogz.net/JS/Classes/CreatingAnonymousFunctions.html

dudeshouse
11-08-2006, 06:50 PM
Thank you thank you thank you.

Awesome, exactly what I wanted.

And thanks for the timely reply.

Glenn



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum