0

i have a simple button in my aspx page:

<asp:Button ID="a1" runat="server" OnClientClick="test2();" />

and the javascript function is:

function test2() {

    setTimeout("alert('hello')", 1250);

}

The SetTimeout is totaly ignored - the alert won't show and the timer won't wait. i read about 10 posts about this problem on ths site , none of the solutions work. i tried calling another function instead of the alert function, i tried calling function with parameters using the function(){..}. i also tried calling the Settimeout straight from the OnclientClick function :

OnClientClick="setTimeout('alert(\'hello\')',1250);"

nothing works, the Settimeout is ignored! i'm using ie9.

Rodniko
  • 4,926
  • 20
  • 69
  • 93
  • 1
    Do you get an error in the console? – James Hill Nov 24 '11 at 16:19
  • 1
    Have you tried a simple alert('') to see if that event is even being executed. – Sandeep Bansal Nov 24 '11 at 16:20
  • @james Hill : how can i see an eeror in the console? – Rodniko Nov 24 '11 at 16:27
  • @Sandeep Bansai: yes , the function is called , i even set a breakpoint and debuggged it. it just skip the setTimeout row. no alert, no delay. – Rodniko Nov 24 '11 at 16:29
  • 1
    have you tried `window.setTimeout()`? maybe there is some closure problem – aorcsik Nov 24 '11 at 16:30
  • !inti - yes tried that too... didn't work – Rodniko Nov 24 '11 at 18:16
  • ok , i think i found something, but i dont understand: this code doesn't cause a postback and therefore the browser have time to wait for the timeout to be over and the message is shown, it works :. This code causes a postback that by the time it ends the timeout is not over yet and doesn't show the message - ignore it: why is that? i don't understand why without postback the settimeout works and with it doesn't.... – Rodniko Nov 24 '11 at 21:31

3 Answers3

2

The JavaScript worked fine. The page just looks the same, and the javascript just looks like it didn't work, but everything is working as designed.

Here's what’s happening...

User clicks the button. The JavaScript is executed. The settimeout is set and starts to time. The runat="server" now posts to the server (however the timeout was still ticking down) Code for the objects OnClick event is run at the server (the page is being rebuilt server side). A new response stream is sent to the client browser. The client browser catches it and renders the "new" page resetting javascript for the new page (this is the new ASPX page you asked for!).

So even runat="server" on a different form element will stop both settimeout or setinterval methods from finally executing.

IMO a better way to code is to use AJAX on the client side to post up values to your server, catch them on a page at the server, build your html or javascripts and send them back to the client browser to catch and either replace the innerHTML of an element id or eval the javascript... less overhead, faster, better, more graceful and far more control . You can use stringbuilder if your using .NET to build the HTML fragments as this is far faster than multiple string concatenations.

hope this helps.

2

Please try:

window.setTimeout(function () { alert('hello'); }, 1250);
Rodolphe
  • 1,689
  • 1
  • 15
  • 32
  • 1
    Then I guess we need more code. Something somewhere is breaking the code. What deos the console say? (Open the console with Ctrl+Shift+j on Chrome.) – Rodolphe Nov 24 '11 at 18:35
  • ok , i think i found sonthing, but i dont understand: this code doesn't cause a postback and therefore the browser have time to wait for the timeout to be over and the message is shown, it works :. This code causes a postback that by the time it ends the timeout is not over yet and doesn't show the message - ignore it: why is that? i don't understand why without postback the settimeout works and with it doesn't.... – Rodniko Nov 24 '11 at 21:30
  • I don't understand your last comment, sorry. Can you create a live sample and give us the URL? – Rodolphe Nov 25 '11 at 15:28
0

It's 100% Working Try this..

    $(document).ready(function () {
        $('#<%= DoesNotwork.ClientID %>').click(function (e) {
            e.preventDefault();
            setTimeout(function () {
                alert("Hello");
                $("form").submit();
            },
            3000);
        });
    });