0

I have a javascript function that is using setTimeout. My problem is that I have an ASP.NET page that has code that is getting executed on page load before the function is complete. Is there a way to either pause the page load or call the page load only after the timer is finished. The setTimeout file is in a seperate JS file and not part of asp.net page.

setTimeout(function () { myFunc(); }, 1000);

I know in MVC you don't have to deal with this.

tereško
  • 58,060
  • 25
  • 98
  • 150
Kevlon Don
  • 11
  • 2
  • 2
    `Page_Load` is executed server-side before anything is rendered on the client. You can't set a *client-side* wait state to delay the *server-side* code which *created* that client-side wait state. (And in MVC you most certainly do have to deal with the separation of client-side and server-side code.) – David Sep 30 '14 at 17:08
  • 2
    Unless I am mistaken about page lifecycle... Page_Load in code behind will *always* complete before a setTimeout javascript function within a document ready – Kritner Sep 30 '14 at 17:08
  • You can accomplish setTimeout functionality by using 2 ASP.NET pages. First Page use setTimeout then redirect to second page. Or use AJAX instead of second page. – yW0K5o Sep 30 '14 at 17:10
  • Where is the code that it is executing on load? Is it in the .cs file, or another javascript file? – Ruslan Sep 30 '14 at 17:10
  • 1
    Its not like ASP.NET generate part of the page, send it to the client and then generate another part....it generates the whole page and send to the client which by the time client's javascript runs the page is already loaded.. – Steve Sep 30 '14 at 17:11

2 Answers2

0

To pause the Page_Load use System.Threading.Thread.Sleep(1000); in the Page_Load.

Unless you know how to use multiple threads I wouldn't really do this. I would instead disable all Page controls found here: How do I disable all controls in ASP.NET page? then re enable when you wish.

This being said Javascript can't run before Page_load. Most things you can do in Javascript can be replicated in asp.net. But you have no code visible so I can't really assume.

Community
  • 1
  • 1
Hillboy
  • 472
  • 6
  • 19
0

I assume you are referring to the client-side asp.net ajax pageLoad() method (and not the server-side Page_Load method). MS ajax looks for and invokes this method after any postback or partial page postback.

Since pageLoad() fires after any postback or partial page postback you can't delay it. I believe what you want to do is move your page postback or partial page postback call inside your "myFunc()" method, but you would need to post a fuller example of what you are trying to do.

Tom Regan
  • 3,580
  • 4
  • 42
  • 71