-2

I am having a hard time with setTimeOut function .Let me show the code first.

Here is my code :-

function submitform(loginUrl, username, password) {
    try {
        loc = new String(window.location);
        document.forms.frm_login.action = junctionUrl;
        document.forms.frm_login.username.value = username;
        document.forms.frm_login.password.value = password;
        document.forms.frm_login.submit();
        setTimeout(gotoHomePage,4000);
    }
    catch (e) {
    alert(e.message +"submit form");
    }

}

    function gotoHomePage()
    {
      alert("test");
      var url = "test.aspx";
      window.location=url;
    }

But here the gotoHomePage function is not at all triggered after the specified 4 seconds.

What i am doing wrong here.Please suggest .

Any help will be appreciated.

Thanks

Vipin Nair
  • 517
  • 4
  • 9
  • 33
  • Can you place setTimeout(gotoHomePage,4000); before the document.forms.frm_login.submit(); and try ? – AAhad Oct 13 '14 at 05:31
  • When did the clock start? Is this script running after document ready event or is the clock starting when the page is being parsed? – Wayne Oct 13 '14 at 05:32
  • I will try that ,but could you please suggest me why to do so,thanks – Vipin Nair Oct 13 '14 at 05:33
  • @Wayne ;i am calling this from my aspx page ,then this event is been fired – Vipin Nair Oct 13 '14 at 05:34
  • @AAhad : i tried your suggestion ,but it havent worked – Vipin Nair Oct 13 '14 at 05:34
  • @VipinNair My connected died 1/2 way through tring to post the comment... or after the submit has already changed the URL. If you are attempting a redirect after posting data its to late. You can instead post the data using ajax or use the php on the server to do the redirect. – Wayne Oct 13 '14 at 05:41
  • are you calling the `submitform` function from somewhere, or is it just on your page? – ps2goat Oct 13 '14 at 05:44
  • My connection is killing me. You may also be able to submit the form to a target url / an iframe. – Wayne Oct 13 '14 at 05:46
  • @ps2goat : i am calling it from my aspx code – Vipin Nair Oct 13 '14 at 05:59
  • After form submit it refreshes the page so yes right you have to use Ajax.. read this http://stackoverflow.com/questions/374644/how-do-i-capture-response-of-form-submit – AAhad Oct 13 '14 at 06:05
  • Okay @AAhad : i will try it out – Vipin Nair Oct 13 '14 at 06:10
  • @VipinNair - Be careful when using `new String`. `new String` does not return a string, just saying. – Derek 朕會功夫 Oct 13 '14 at 06:26
  • you'll have to show the relevant aspx/html code. ASP.NET webforms adds its own code for postback, in which case your function would be called, but the form would post back before you'd see anything from your call to `setTimeout`. JavaScript is asynchronous, so the postback to the server would be initiated before the `setTimeout` is called. – ps2goat Oct 13 '14 at 14:57

1 Answers1

0

form submission is redirecting to the result page unloading the current page. So, the timer doesn't tick.

Use ajax or set target of the form to iframe. This way, the current page doesn't unload and so timer ticks executing your timeout function.

<form target="form_output">
    <!-- other inputs -->
</form>

<iframe name="form_output"></iframe>
gp.
  • 8,074
  • 3
  • 38
  • 39