Need help with setTimeout

M

MG

I am a newbie.

On this page:
http://www.blighty.co.za/resetform/
I am testing a script to reset a form after it has been submitted.

I have this line:
setTimeout('mf.reset()',10000);
There should be a 10 second delay before the form resets. But the form
resets almost immediately.

I have stared at the code for a long time and just cannot figure out why
there is no 10 second delay.

Hope someone can help.

Thanks
MG
 
G

Gregor Kofler

Am 2010-06-20 20:13, MG meinte:
I am a newbie.

On this page:
http://www.blighty.co.za/resetform/
I am testing a script to reset a form after it has been submitted.

I have this line:
setTimeout('mf.reset()',10000);

mf.reset() is executed immediately and the result becomes the argument
for the setTimeout method.

window.setTimeout(mf.reset, 10000);

is what you want.


Gregor
 
L

Luuk

Op 20-6-2010 20:13, MG schreef:
I am a newbie.

On this page:
http://www.blighty.co.za/resetform/
I am testing a script to reset a form after it has been submitted.

I have this line:
setTimeout('mf.reset()',10000);
There should be a 10 second delay before the form resets. But the form
resets almost immediately.

I have stared at the code for a long time and just cannot figure out why
there is no 10 second delay.

Hope someone can help.

Thanks
MG

change 'return true' to 'return false'
so your form does not get submitted,
which clears the form

there is an additional bug in the function,
but my knowledge of javascript will not allow to solve it now ;)
 
G

Garrett Smith

I am a newbie.

On this page:
http://www.blighty.co.za/resetform/
I am testing a script to reset a form after it has been submitted.

That sounds like an unpleasant user experience.
I have this line:
setTimeout('mf.reset()',10000);

function clearForm(mf){
setTimeout('mf.reset()',10000);
return true;
}

What happens when you return true from an event handler?
https://dev.mozilla.jp/localmdc/dev...re_event_handlers.html#Prevent_Default_Action

What scope does the host method `setTimeout` use?
http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx

MDC and MSDN, as well as many other resources, are linked from the FAQ.

Please see: http://jibbering.com/faq/

Garrett
 
G

Garrett Smith

Am 2010-06-20 20:13, MG meinte:

mf.reset() is executed immediately and the result becomes the argument
for the setTimeout method.

No, that is not what happens.

When setTimeout is supplied a string argument, as is the case with
'mf.reset()', the string is evaluated after pausing for the delay given
as the second parameter, in this case 10000 ms.

setTimeout("alert(1)", 10000);

In the OP's example, the function is not called for a different reason.
The OP is still trying to figure that out, so I'm not going to give that
reason away.
window.setTimeout(mf.reset, 10000);

is what you want.

Passing the value of `mf.reset` to setTimeout will cause the function to
execute in 10000 ms with a null base object. That cannot be expected to
work reliably.

It will submit the form in most versions of IE, but will result in an
error in other implementations. MSIE is a notable exception, as its host
objects tend to retain a reference to their base object.

http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx

Garrett
 
M

MG

Gregor Kofler said:
Am 2010-06-20 20:13, MG meinte:

mf.reset() is executed immediately and the result becomes the argument for
the setTimeout method.

window.setTimeout(mf.reset, 10000);

is what you want.


Gregor

I tried your suggestion (plus a few variations), but still there is no 10
sec delay.

MG
 
M

MG

Luuk said:
Op 20-6-2010 20:13, MG schreef:

change 'return true' to 'return false'
so your form does not get submitted,
which clears the form

I want the form to be submitted. Then, after a delay, the form is to be
reset.

there is an additional bug in the function,
but my knowledge of javascript will not allow to solve it now ;)

My knowledge of javascript doesn't allow me to solve it either :(

MG
 
M

MG

Garrett Smith said:
No, that is not what happens.

When setTimeout is supplied a string argument, as is the case with
'mf.reset()', the string is evaluated after pausing for the delay given as
the second parameter, in this case 10000 ms.

setTimeout("alert(1)", 10000);

In the OP's example, the function is not called for a different reason.
The OP is still trying to figure that out, so I'm not going to give that
reason away.

Yes, I am still trying to figure that out. I would appreciate it if you did
give the reason away.

MG
 
G

Garrett Smith

I want the form to be submitted. Then, after a delay, the form is to be
reset.
How is that going to be possible?

What do you think happens when you submit the form?

Garrett
 
E

Evertjan.

MG wrote on 20 jun 2010 in comp.lang.javascript:
I want the form to be submitted. Then, after a delay, the form is to be
reset.

That can only be done, if you submit the form to another window,
otherwise there is NO form of the old page to be reset,
because that page is gone and exchanged for a new version
with a new form.

Try:

<form
onsubmit = "setTimeout('resetMe()',10000);";
target = 'anotherWindow';
However, submitting to the same window automagically
"resets" the form without the need of any javascript,
because you will see a new empty form.
 
M

MG

Evertjan. said:
MG wrote on 20 jun 2010 in comp.lang.javascript:


That can only be done, if you submit the form to another window,
otherwise there is NO form of the old page to be reset,
because that page is gone and exchanged for a new version
with a new form.

Try:

<form
onsubmit = "setTimeout('resetMe()',10000);";
target = 'anotherWindow';


Thanks, this has helped me.

I am still learning javascript. I found a php code on the internet I want to
use to return the form as email. After a few test I now see what is
happening.

(I start on php next month)

MG
 
T

Thomas 'PointedEars' Lahn

Gregor said:
MG meinte:

mf.reset() is executed immediately and the result becomes the argument
for the setTimeout method.

No, it is not; notice the string literal? What you are describing would
happen with

setTimeout(mf.reset(), 10000);
window.setTimeout(mf.reset, 10000);

is what you want.

No, since the calling object would be the global or `window' object then; if
this had a chance to work in the first place, a function expression or an
equivalent Function instance reference would be needed for the first
argument:

window.setTimeout(function () { mf.reset(); }, …);

But the problem here is trying to access the form object (approximately 10
seconds) after it has been deconstructed, since the response to the POST
request is displayed in the same window (the `submit' event is not being
cancelled). It might work if either the `submit' event was cancelled or
clearForm(document.forms[0]) was called in the `onload' attribute, but that
would be a Bad Thing.


PointedEars
 
G

Garrett Smith

Gregor said:
MG meinte:
[...]

window.setTimeout(function () { mf.reset(); }, …);

But the problem here is trying to access the form object (approximately 10
seconds) after it has been deconstructed, since the response to the POST
request is displayed in the same window (the `submit' event is not being
cancelled). It might work if either the `submit' event was cancelled or
clearForm(document.forms[0]) was called in the `onload' attribute, but that
would be a Bad Thing.

A doing something onload is different than doing something after a form
submission. Onload affects any request type (GET as well as POST).

And yes, the form submission to the same window was preventing that
setTimeout to run, but the OP seems to have figured that out by now.

Garrett
 

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
474,078
Messages
2,570,570
Members
47,204
Latest member
MalorieSte

Latest Threads

Top