Timer control

  • Thread starter arunkumar_m2001
  • Start date
A

arunkumar_m2001

Can Somebody help me to code a timed test. Actually I want to implement
a timer in a web application which will pop-up an alert automatically
by refreshing the web page after a specific time.

Thanks
Arun
 
A

arunkumar_m2001

Correction:

Can Somebody help me to code a timed test. Actually I want to implement

a timer in a web application which will pop-up an alert automatically
by refreshing the web page after a specific time in ASP.NET.


Thanks
Arun
 
K

Kevin Spencer

That would be a client-side timer, which can be achieved with an HTML META
Refresh tag or JavaScript (using the setTimeout() method).

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
A

arunkumar_m2001

Kevin,
Thanks for your reply. I used the meta tag to refresh the page.
My application is a timed test one. I need to capture the current time
when the user access the application on page load. And after an hour
the application should pop-up an alert saying time expired by comparing
the current time with the time when the user started the test.

Refresh tag just refreshes the page, but I couldn't compare the times
because when the page reloads the current time changes to the latest
time which is obvious, can you suggest me a solution for this.

Thanks again
Arun
 
K

Kevin Spencer

Use the JavaScript setTimeout() function to launch the popup window. This
function takes 2 parameters: a string that contains the function call, and
the number of milliseconds to wait. A second is 1000 milliseconds, and there
are 3600 seconds in an hour, so you want to set the milliseconds to 3600000.
Example:

<script type="text/javascript"><!--
setTimeout("loadPopupFunction()", 360000);
// --></script>

Note that this doesn't need to know the time. It will fire 1 hour after the
page loads. If the page reloads at this time, the timer is restarted when
the page returns to the client.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
A

arunkumar_m2001

Thanks Kevin,

It really helped. Now I could pop up an alert after the specific
time.Now it's on the client side. But,I want to execute server side
script when the time expires.
For eg: If the time expires in the test the alert should appear,no
matter what the user does whether he clicks ok in the alert pop-up or
ignores it, the server side script should execute and submit the
answers to the database.

In other words after the time expiration I need to trigger an event
without clicking any buttons(or controls).

Thanks again
I appreciate your help
Arun
 
K

Kevin Spencer

Hi Arun,

All you need to do is programmatically (via JavaScript) submit the form.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
A

arunkumar_m2001

Thanks Kevin,

Is it possible to execute the server side script via javascript
submit.Can you help me with some sample code/

Thanks for all your help
Arun
 
A

arunkumar_m2001

Thanks Kevin,

Is it possible to execute the server side script via javascript
submit.Can you help me with some sample code/

Thanks for all your help
Arun
 
K

Kevin Spencer

The Page executes every time the form is submitted. I'm not sure what you're
having trouble with.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
A

arunkumar_m2001

Hi Kevin:

The page executes successfully. But when it executes I need to check if
the session is valid in the code behind and do some action event.

Thanks
Arun
 
A

arunkumar_m2001

Kevin,

Basically when the time expires I should abandon the session and also I
need to write in a way that it checks the session and do some action
events(i.e like redirecting to different page etc..) I dont see how I
can do session abandon in javascript

Thanks
Arun
 
K

Kevin Spencer

You don't abandon Session on the client. You submit the form using
JavaScript and do your work on the server when the Page posts.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
A

arunkumar_m2001

Kevin,

In that case I need to compare the time.. like the current time and the
test start time which makes the setTimeout property in Javascript of no
use.

My exact thing is I need to call a function that inserts a record into
the database when the page loads only when the Time Expires. So again I
need to compare the timings as discussed before levaing the setTimeout
property of no use.

Thanks for you help again
Arun
 
K

Kevin Spencer

Your client-side script is going to submit the form after a specific time
interval. If you need to distinguish between a client-side Event causing the
PostBack, and your client-side timer posting back, add a hidden form field
to the page, and have your script insert a value into it prior to submitting
the form. If the hidden form field has a value, it was submitted as a result
of the timer. This is similar to the way a Server Control raises a
server-side event from a client-side event. In fact, you could even create a
Server Control that does all this. But unless you want to re-use it in
another app, this would be simpler to accomplish.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
A

arunkumar_m2001

Hi Kevin:

Thanks for the reply. It sounds good.But in the quiz, when the user
clicks next button, the page reloads everytime when it's clicked,so
during the postback the Javascript loadpopupfunction() executes
evreytime starting the timer for every single click.That's where I have
problems, if there's something like notpostback method as in server
side script it will be really helpful.

Is there anyway I can have a method that could start the timer only
once.

Thanks again
Arun
 
K

Kevin Spencer

I believe I outlined a way to do this with a hidden form field.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
A

arunkumar_m2001

I don't really understand what you said.
Actually in my codebehind server script - I get the start time and
store it in a session variable
Sub PageLoad()
** During load and not postback
If Not IsPostBack Then
Session("ExpTime") = Now().AddMinutes(5)
lblTimeDisplay.Text = "This is a 5 minute quiz.Your quiz
will expire by " & String.Format("{0:t}", Now().AddMinutes(5))
end if

** During load and postback

If Now() > Session("ExpTime") Then
SubmitAnswers()
End If

End Sub

The above code works fine, and the answer gets submitted perfectly when
the time is expired and a user clicks a submit,next question or
previous question(any click event that causes postback)

In my Javascript code
<html>
<head>
<script language="javascript">
function loadPopupFunction()
{
alert("Time Expired")
document.Form1.submit();
}
</script>
</HEAD>
<body onload="setTimeout('loadPopupFunction()',300000)"
MS_POSITIONING="GridLayout">


The timeout works fine but it will restart everytime when the page
loads.I need to get the alert exactly when the time expires which on
click will eventually submit the form.

The hidden field that you said I couldn't properly understand.
Sorry!
Thanks for all yuor help
Arun
 
K

Kevin Spencer

Okay, you only want to have this timeout execute once, right? Well, first of
all, you don't need to do any checking on the server. The client side will
submit the form when the time is up. Second, since you only want to start
the timer once, when the test starts, you don't want to put the JavaScript
into the onload attribute of the body. When you are ready to start the test,
from the server side, add the timeout JavaScript to the page using
Page.RegisterStartupScript(). Only do this when the user is ready to start
the test. Put a hidden form field into the form, and set it to a blank
value. The client-side script will put a string into the hidden form field
before it submits the form. All you have to do on the server side is check
the hidden form field whenever the Page is Posted Back. If the value of the
hidden form field is not blank, the timer posted the Page back, so you can
submit the answers.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top