Skip to content Skip to sidebar Skip to footer

Settimeout() Javascript Function Is Ignored

i have a simple button in my aspx page: and the javascript function is: function test2() { setTimeout('al

Solution 1:

Please try:

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

Solution 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.

Solution 3:

It's 100% Working Try this..

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

Post a Comment for "Settimeout() Javascript Function Is Ignored"