I’m sure I’ll hate myself when I find out what the issue is, but I can’t for the life of me figure out why this won’t work. I’m using setTimeout elsewhere in the site just like this and it is working fine.
<input type='button' value='Submit Form' onclick='ValidateForm()'/>
var submitFinalTimeout;
function ValidateForm() {
//Performing a number of validations which may return false;
//I know that the following if condition evaluates as true. But just for giggles I tried moving it outside of the if and it still never fires.
if (types.indexOf("K2") != -1) {
submitFinalTimeout = window.setTimeout(function () { alert("Timeout set"); }, 2000);
validateK2();
return true;
}
return true;
}
When I say validate, I mean I am alerting the use if an issue is found and returning false to prevent the validation from continuing. The return values of true or false do not affect whether or not page posts.
I’ve tried also removing everything else inside of the curly braces to ensure no other code was conflicting with it. I have literally tried removing ALL other code withing the ValidateForm function except for the setTimeout and it still never fires.
I saw a similar post where the issue was some other javascript error on the page preventing it from working. But neither my developer console in Chrome nor firebug show any errors or any kind on the page. Just a few warnings about interpreting an image.
I’m going to guess that
ValidateFormis called when a form is being submitted. The reason you’re not seeing the function called bysetTimeoutis that the page is torn down completely by a form submission, and then replaced by the result.