1

Here I want do timout in tout times, but setTimeout() is not using tout as timeout parameter. How it could be solved.

<script language="JavaScript" type="text/javascript">
var tout=10*1000;
var t = setTimeout(document.myform.submit(),tout));
</script>
dfsq
  • 191,768
  • 25
  • 236
  • 258
NU113R
  • 101
  • 1
  • 9
  • possible duplicate of [why is this setTimeout not working](http://stackoverflow.com/questions/5116223/why-is-this-settimeout-not-working) – Felix Kling Apr 05 '14 at 10:16

3 Answers3

3
var tout = 10 * 1000;
var t = setTimeout(function() {
    document.myform.submit();
}, tout);

Read about how to use setTimeout properly here. The first argument should be a function reference and the second timeout in milliseconds.

dfsq
  • 191,768
  • 25
  • 236
  • 258
2
var delay = 60000;

var timer = setTimeout(function() {
  document.myform.submit();
}, delay);

Or even better

var delay = 10000,
    f     = document.myform,
    timer = setTimeout(f.submit.bind(f), delay);

Note: Function.prototype.bind depends on ES5.

maček
  • 76,434
  • 37
  • 167
  • 198
0

The problem you have is that you call the submit function on the setTimeout line and pass the result of calling it to setTimeout.

What you want to do is pass a reference to a function to setTimeout:

var t = setTimeout(document.myform.submit, tout));

Unfortunately that doesn't quite cut it, because the submit function doesn't know what form it was called on. You also need to bind the context:

var t = setTimeout(document.myform.submit.bind(document.myform), tout));

Function.prototype.bind returns a reference to a function.

Tibos
  • 27,507
  • 4
  • 50
  • 64