Rodniko
Rodniko

Reputation: 5124

Settimeout() javascript function is ignored

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.

Upvotes: 0

Views: 4579

Answers (3)

Sagar Jadhav
Sagar Jadhav

Reputation: 129

It's 100% Working Try this..

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

Upvotes: 0

stephen west
stephen west

Reputation: 21

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.

Upvotes: 2

Rodolphe
Rodolphe

Reputation: 1731

Please try:

window.setTimeout(function () { alert('hello'); }, 1250);

Upvotes: 2

Related Questions