tags:

views:

46

answers:

4

I have the following:

var thiscode = {
init: function(){
    jQuery('#submit').bind('click',thiscode.clickListener);
},

clickListener: function(event){
    setTimeout('document.myform.submit()',5000);
    return false;
}
};
thiscode.init();

setTimeout is working, but when it fires, I get this: "document.myform.submit is not a function".

Any idea why, and/or how to fix it?

A: 

setTimeout can also take a function handle - try passing document.myform.submit without the quotes:

setTimeout(document.myform.submit,5000);
Gus
I wouldn't expect that to work. The `submit` function will be called without the correct context (`this` value), at least in theory (since it's a host-provided function, it may not behave exactly like a genuine JavaScript function, but that would be implementation-dependent).
T.J. Crowder
+3  A: 

Don't pass a string to setTimeout, pass an anonymous function...

clickListener: function(event) {
  setTimeout(function() {
    document.myform.submit();
  }, 5000);
  return false;
}

EDIT: Just had a revelation. If you have an element in your form with a name/id of "submit", that will override the submit function in the DOM (document.myform.submit will now refer to that element instead of the function). If this is the case, you'll need to rename that element.

Josh Stodola
Ha! Brilliant. You and David had it exactly. Just had to rename the element. Thank you!
mjsiemer
@msjiemer Please accept David's answer as the official answer. He discovered that before I did.
Josh Stodola
A: 

David's answer is good, but you really should not be binding click, what if the user presses enter to submit the form? They've just gone around your procedure.

Instead bind .submit(). You can use a variable to control if there is a submit:

var submitNow = false;
$(function() {  
      // Set up the submit handler
    $("form").submit(function() {

          // In 5 seconds...
        setTimeout(function() {

              // Set the submit variable to true
            submitNow = true;

              // Trigger the submit
            $("form").submit();
        }, 5000);

          // This will only submit if submit variable is true.
        return submitNow;
    });
});​

Try it out with this jsFiddle example

Peter Ajtai
I don't believe this will work as expected because `$(this).closest("form").submit` refers to the submit event handler in jQuery, not the submit event of the DOM element. I think he would need `$(this).closest("form").get().submit`
Josh Stodola
@Peter Negative http://jsfiddle.net/stodolaj/Etv65/
Josh Stodola
@Josh - On second thought, binding only the `click` of an element to control a form's submital is a bad idea. You can submit forms with the `enter` key too.
Peter Ajtai
Good point Peter. Thanks. I'll make that adjustment.
mjsiemer
+3  A: 

This likely has nothing to do with your use of setTimeout (which has issues raised by other people).

The most likely cause is that you have an element named submit (or with that as an id) inside the form (probably the one that you are matching with your jQuery selector) and that this has replaced the function that was on the submit property with an HTMLElementNode.

The easiest solution is to rename the element.

David Dorward
+1 Yeah this just hit me too, I think you are right.
Josh Stodola
Thanks a ton! That did it.
mjsiemer
Also, you shouldn't be binding to the `click` handler of your submit button, whatever it's called, since form can be submited in other ways... like pressing enter. Instead bind to the `.submit()` of the form. For a delayed submit, I think you'll have to use a variable.
Peter Ajtai