El Sa7eR
El Sa7eR

Reputation: 921

Timer with javascript and C#

I need to create a timer in an ASP.NET web page.

I have tested some JavaScript code like:

window.onload = function WindowLoad(event) {
    setTimeout(function () { alert("DoIT") }, 60000);
}

and it works like expected but when I replace the alert with a call to a C# function like:

window.onload = function WindowLoad(event) {
    setTimeout(function () { <% doIt(); %> }, 60000);
}

the function works on the load of the page and not after the specified period.

Upvotes: 0

Views: 152

Answers (2)

Vladimir Bozic
Vladimir Bozic

Reputation: 444

Try this blog post, this should be what you are looking for: http://deebujacob.blogspot.com/2012/01/aspnet-ajax-web-method-call-using.html

It's like other people sad you can not call server side functions directly from javascript you have to make a request, that's is way good God gave as ajax :)

Upvotes: 0

nozari
nozari

Reputation: 504

<% doIt() %> runs during the server side process, not on client. If you want to do something on the server side you should create a webmethod and make a post to it.

Upvotes: 3

Related Questions