Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

consequetive events + global variables

Status
Not open for further replies.

luyckxs

Programmer
May 5, 2004
1
BE
Below you find a small test page.
I contains an input box, with an onBlur event, and a form, with an
onSubmit event.

When I place the cursor in the text box and push ENTER, first the
onBlur event and then the onSubmit event is raised.
The onBlur event displays the value of the global variable [true], it
changes the value of the global variable to [false] and displays it
again [false].
The onSubmit event simply displays the value of the global variable
[true], which is the original value, not the new. Huh?

Why doesn't the onSubmit event notice that the global variable has
changed?

<html>
<head>
<script language="javascript">
var something = true;
function BlurFunc() {
alert('begin BlurFunc: ' + something);
something = false;
alert('begin BlurFunc: ' + something);
}
function SubmitFunc() {
alert('begin SubmitFunc: ' + something);
return false;
}
</script>
</head>
<body>
<form name="frmTest" method="post" action="steven.asp"
onSubmit="return SubmitFunc();">
<input type="text" name="test" onBlur="BlurFunc();">
<input type="submit">
</form>
</body>
</html>
 
The onSubmit event built its alert() message before the global changed state. When the Submit event built its alert() message the global variable was True, but by the time the Submit message finally got to the top of the task queue the global state had changed. Such is asynchronous life.

Program narrative -- pressing ENTER schedules a Blur event and a Submit event. The Blur event blocks on the first alert(), global is True. The Task Manager starts the Submit event which builds an alert() message, global is still True, and blocks.

Eventually the Blur event alert() unblocks. It changes global to False and alerts again. When the Task Manager finally gets around to activating the Submit alert() the state of global has changed. The Submit message is accurate when it was processed, inaccurate when it displays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top