Make a countdown timer?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all,

Can anybody tell me, or point me the right direction on how I would make a
simple countdown timer from 60 sec down. I also need to control the time
from a server.

Thanks!

Rudy
 
Hi Rudy,

I believe that you meant to post this to the ASP.NET ng....but I will answer
your post anyway.

To create a countdown timer you will need to use javascript (or a client
scripting language) to accomplish this. If you use javascript you can use
the setTimeout function to create a timer. As for the date and time you can
simply call the System.DateTime namespace to get the current time. I have
posted a sample of both for you below.

I hope this helps.
----------------------------
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<SCRIPT LANGUAGE="Javascript">
var SecondsToCountDown = 60;
function CountDown()
{ --SecondsToCountDown;
//set the value of the textbox
document.Form1.TextBox1.value = SecondsToCountDown;
//how long to wait to call CountDown again
setTimeout("CountDown()", 1000); //last number is in milliseconds
if(SecondsToCountDown == 0)//if the time is up
{
alert("Your time is up!");
}
}
</SCRIPT>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
}
</script>

</HEAD>
<body onload='CountDown()'>
<form id="Form1" method="post" runat="server">
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P>
<asp:Label id="Label1" runat="server" /></P>
</form>
</body>
</HTML>
 
Hi Sahil and Brian!

Thnks for the response. Brian, I'm using vb.net/ado to build this
application. So I don't think java would help, besides, I never used Java
yet, so I'm sure I would be more lost with it than I am now. Is Java hard to
learn, even if you only have a couple of years of programing under your belt?

Sahil, basicly the big picture it have a time set up to allow a user to
answer a question. Just like the one when you take a microsft test, a little
timer in the corner. But I need to be able to stop and start the timer from a
server program. The timer needs to be sync to all of the client computers,
up to 50.

Thanks!!!

Rudy
 
Rudy,

I think Brian meant Javascript, which is much easier, and different than
Java.

If it's a web based app, you could easily do a settimeout function on the
client using Javascript, the hacker client might disable javascript to get
around that, so you have to send a "Question Generated" time to the page in
some encrypted fashion, and compare the submission time - which of course
someone will bitch about being unfair on a low bandwidth connection, but
that is a limitation of web you'll have to live with.

The second case could be, if it is a windows app. A windows app could be
controlled by the server using a simple remoting like scenario, where the
client becomes the remoting server for a breif call. The server calls a
function to indicate the start of a timecycle, and you might want to
implement checks like above. This could even be done using webservices.

HTH,


--

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/
 
Hi Rudy,

Sahil is correct. The code sample I provided is javascript not Java. They
are very different languages. Javascript is much easier to learn and is much
more forgiving (but in some cases harder to debug).

If this is a web app you will have to use a client script to emulate the
timer. You cannot force a page refresh from the server. This is why you
have to use javascript (or any other client scripting language). You can use
the script that I provided in the sample to accomplish what you are
attempting to do, although I would take Sahil’s advice about coding around
hackers.

The problem that you are going to encounter with the synchronization is that
the server cannot force the client to do anything. Communication has to be
initiated from the client. I think that one way of keeping the pages in sync
is to have an intermediate page that will refresh at some interval (e.g. with
a META refresh). On this page you would keep refreshing it until some
variable gets populated. At this point the page would be redirected to the
timed page. Keep in mind this is just a theory. The real solution may or
may not be more complicated than this.

Good Luck!
 
Hi Brian and Sahil,

Thanks for the great advice, at least it gives me something to start with.
I also wanted to make my application a little bit more clear, so if there is
a better way. Sorry for not being so detailed before. This windows
application will not be on the internet, but a intranet.The client computers
are set up so the only application that is running is the client application.
The user won't have access to the desktop, won't be able to quit the program,
or use the computer in any other way, except to runthe application, and
answer the questions in a timely manner. So there will be 100% control. The
computer itself won't even be by the user, but in a a diffrent room. so the
user can not do anything except answer the question, or let the application
time out.

Thank you for all your time and help!

Rudy
 
Back
Top