setTimeout doesn't work

A

Aaron Fude

Hi,

I've inherited this mechanism for logging in in a web application:

<form name="LoginForm" action="WillNotGoHere.jsp"
onSubmit="SubmitImage.src='LoginImage.jsp?Trigger='+pRandomString() +
'&Password='+LoginForm.Password.value;
setTimeout(window.location.reload(true), 500);" method="POST">

I think it's self-explanatory!

The problem is that setTimeout(window.location.reload(true), 500);
doesn't wait for half a second and instead reloads immediately
preventing the login mechanism from completing. (It does complete on
the server eventually, but too late.)

What gives?! and how to fix it?

Many thanks in advance!!!
 
V

VK

Hi,

I've inherited this mechanism for logging in in a web application:

<form name="LoginForm" action="WillNotGoHere.jsp"
onSubmit="SubmitImage.src='LoginImage.jsp?Trigger='+pRandomString() +
'&Password='+LoginForm.Password.value;
setTimeout(window.location.reload(true), 500);" method="POST">

I think it's self-explanatory!

The problem is that setTimeout(window.location.reload(true), 500);
doesn't wait for half a second and instead reloads immediately
preventing the login mechanism from completing. (It does complete on
the server eventually, but too late.)

What gives?! and how to fix it?

onsubmit handler is a "submission permission request":
return true / return undefined - go ahead
return false - cancel submission

Your handler returns undefined, so the form gets submitted, page
reloaded, all timers are reset.

Add ";return false;" as the last statement in your handler.

P.S. That is aside of a highly risky business to depend on hardcoded
timeouts in the Web. It would be much nicer to rebuild it on job-
confirmed-done-do-next basis.
 
L

Lasse Reichstein Nielsen

Aaron Fude said:
I've inherited this mechanism for logging in in a web application:

That suggests you won't get any more code from the same source.
Good thing :)
<form name="LoginForm" action="WillNotGoHere.jsp"
onSubmit="SubmitImage.src='LoginImage.jsp?Trigger='+pRandomString() +
'&Password='+LoginForm.Password.value;

so you try to send an asynchronous message to the server by loading
an image with a specially crafted URL. A message that contains the
user password. It's some kind of asynchronous login.
setTimeout(window.location.reload(true), 500);" method="POST">
I think it's self-explanatory!

I think the intention is somewhat clear. The result is also expectable.

setTimeout(window.location.reload(true), 500)

This evaluates "window.location.reload(true)" and then uses the value
of that expression as an argument to setTimeout. Except that
evaluating it reloads the page immediately.

You want either:
setTimeout('window.location.reload(true);', 500)
or
setTimeout(function(){window.location.reload(true);}, 500)
The problem is that setTimeout(window.location.reload(true), 500);
doesn't wait for half a second and instead reloads immediately
preventing the login mechanism from completing. (It does complete on
the server eventually, but too late.)

What gives?! and how to fix it?

Expected behavior. You want to delay the execution of code. To do that,
you must send the code itself as an argument, which means wrapping it
up so that it won't be executed right away. I prefer the function wrapper,
since code-as-string is much harder to debug.

/L
 
L

Lasse Reichstein Nielsen

Aaron Fude said:
<form name="LoginForm" action="WillNotGoHere.jsp"
onSubmit="SubmitImage.src='LoginImage.jsp?Trigger='+pRandomString() +
'&Password='+LoginForm.Password.value;
setTimeout(window.location.reload(true), 500);" method="POST">

Have you considered calling the reload function from the image's
onload or onerror methods instead of trying to guess how long to wait?

Or using XMLHTTPRequest for communication instead of an image?

/L
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top