views:

669

answers:

3

How to call or make the javascript function from the Application_Start of global.asax in asp.net mvc(C#) application?

A: 

"The javascript function gets the data to be shown to the User from database through jquery. The javascript function will be executed periodically using setTimeout"

This wouldnt be the place to do it.

Have you thought about using your masterpage?

Pino
i am using this call in master page now. But its keeps resetting the interval everytime i navigate to the other page. Say i need to call the js function every 1 minute, when i navigate from first to second page, the interval gets reset and i need to wait 1 minute in that page for the js function to get call
Prasad
Thats the nature of the web. Each page is independant. Btw, If I were you I'd use a session to keep track of the time the user turned up at the page. Then use some logic to check the current time vs the session time and then if it breaches 1 min output an alert script (Or whatever you like)
Pino
You should call the function once on $(document).ready) and also via setTimeout. Then it gets called in second 1 and in 60/120/180
Malcolm Frexner
@Mathias Fritsch - he allready stated he's switching between pages. SO that will not work.
Pino
A: 

Since JavaScript executes on client side and global.asax executes on server side. You cannot do that.

çağdaş
I think he was looking for a way to dump it to the page a bit like RegisterClientScript
Pino
@Pino, he says he wants to call the JS function, in both the question and in the comments.
çağdaş
yes. exactly like RegisterClientScript in regular asp.net.
Prasad
@Prasad, Response object won't be ready on Application_Start(), so you can't output anything in there.
çağdaş
Yep, as cagdas says, it wouldnt resolve your issue.
Pino
any idea on to solve this issue?
Prasad
See my post about using the Session to track it. I did this about a year ago, however it got a bit messy. I ended up using a seperate web-app to track it and called that app from the main website.
Pino
+1  A: 

You can remember the last "invoked" time in Session or cookies (which is easier for javascript but worse for performance/etc) and then

function check() {
   // or var lasttime = <%= Session["lasttime"] %>;
   if (now - $.cookie("lasttime") > timeout)
   {
     $.cookie("lasttime", now);
     performAction();
   }
   window.setTimeout(check, 1000);
}

You can call time function once from $(document).ready().

But note that it may take browser several seconds to render page, or it may bump into 404 or other errors and page will be inactive... javascript is not a reliable way to do scheduled actions.

Another way is to have your timer on server. JavaScript function like above will just ask for it from time to time, passing user ID or something like that. This will prevent timer reset during page reload. But you'll have to do request too often. So the best solution would be to combine two techniques:

  1. Run timer on server
  2. When page is renders, set var inited = false;
  3. Run function above but like this: if (!inited) timer = $.getJSON("/timer?uid=x"); and when you have the precise current timer you can continue with JavaScript only, without server requests.
queen3
I like and agree with this answer. If the goal is to prevent some javascript function from being called more than once per minute regardless of how many separate pages the user clicks around during that time, then having javascript be responsible (through a cookie, as shown) for keeping track of the time it last ran makes the most sense. You _could_ use a Session var (which most likely relies on cookies anyway), sure, but you'll just end up injecting it back into the script. Only thing I'm not sure of, is your comment that doing this in javascript cookies is worse for performance?
Funka
Actually, the only thing to watch out for might be if the user has open multiple tabs/windows, to make sure they are not all competing for the timer!
Funka
Cookies are sent with each requests, even for static files. Yes session relies on cookies (usually) but there's single one; if you add more this increases traffic a bit. And user can switch cookies off. As for multipage, this is where combined client-server solution makes sense... as there'll be always single instance of the timer on the server - which can be contacted once or periodically to check the timer.
queen3