how to create timer in jsp?

I am preparing an online examination s/w using jsp and oracle.For every exam session there is a fixed time to be kept eg. 30 mins
after which the page should become inactive.Also the time has to be shown on top of the page which is decremented every second.On each page there is a next button on clicking, the next question is posed.The timer should be for the whole exam session.
Please help and tell me in detail code...
Thanking u. urvi
[ May 30, 2003: Message edited by: urvi23 patel ]

That's a bit of a tough one since you're trying to impose real-time response on top of a protocol (HTTP) which is most definitely not real-time capable. My initial thought is to use a frameset: one frame with the timer and one frame with the questions. The page loaded into the "timer frame" would perform the countdown using Javascript. A variant: assuming you are concerned with modern browsers only, avoid the frameset (which are a thing to be avoided if at all possible) and create a page with an embedded iframe. The "master page" can control the timer, while the iframe is used to serve the question pages. Problems arise with these approaches if the "timer page" gets refreshed. Perhaps the "end time" is saved on the server in the session so that the timer control page can pick up the correct remaining time regardless of refresh. There are many other approaches, but one of the above (I prefer the iframe), is the first approach I'd give a try. hth, bear [ May 30, 2003: Message edited by: Bear Bibeault ]

Another possible approach: Let JavaScript write the remaining time in a hiddenfield or use as display an inputfield. When an answer is submitted, you write it with JSP back to Javascript again. E.g.:

[ May 30, 2003: Message edited by: Christian Baron ]

urvi patel

Greenhorn

Posts: 17

posted 14 years ago

Originally posted by Christian Baron: Another possible approach: Let JavaScript write the remaining time in a hiddenfield or use as display an inputfield. When an answer is submitted, you write it with JSP back to Javascript again. E.g.:

[ May 30, 2003: Message edited by: Christian Baron ]

Thankx for ur. reply... i am doing in that way only but somewhat confused..so now it's clear to me. Again thank you.

urvi patel

Greenhorn

Posts: 17

posted 14 years ago

Originally posted by Bear Bibeault: That's a bit of a tough one since you're trying to impose real-time response on top of a protocol (HTTP) which is most definitely not real-time capable. My initial thought is to use a frameset: one frame with the timer and one frame with the questions. The page loaded into the "timer frame" would perform the countdown using Javascript. A variant: assuming you are concerned with modern browsers only, avoid the frameset (which are a thing to be avoided if at all possible) and create a page with an embedded iframe. The "master page" can control the timer, while the iframe is used to serve the question pages. Problems arise with these approaches if the "timer page" gets refreshed. Perhaps the "end time" is saved on the server in the session so that the timer control page can pick up the correct remaining time regardless of refresh. There are many other approaches, but one of the above (I prefer the iframe), is the first approach I'd give a try. hth, bear [ May 30, 2003: Message edited by: Bear Bibeault ]

What if javascript is disabled by user? You can make a timer bean,and get current time in mili seconds as well as elapsed time using System class methods. give it a try ,I think this would be better approach. Though script is ok,but try to do it with pure jsp would be better approach. Ashish

Chris Baron

Ranch Hand

Posts: 1061

posted 14 years ago

@Ashish

You can make a timer bean,and get current time in mili seconds as well as elapsed time using System class methods

On the clients computer to display the actual remainig time? Show us how !

urvi patel

Greenhorn

Posts: 17

posted 14 years ago

Originally posted by Ashish Kothari: What if javascript is disabled by user? You can make a timer bean,and get current time in mili seconds as well as elapsed time using System class methods. give it a try ,I think this would be better approach. Though script is ok,but try to do it with pure jsp would be better approach. Ashish

Hi.. You made me confused.. so now tell me answer also that,how to display the actual remainig time On the clients computer ? urvi

I was asked in interview ,what if javascript is disabled?then what to do?

Generally it's allways better to get as much as possible done on the Server, but in this special case with a timer...

urvi patel

Greenhorn

Posts: 17

posted 14 years ago

Hi.. For that timer, I had done with that Java Script code.. and i have to display time in minute, so i have done the window.setTimeout( "timer()", 60000 ); So it displayed the time in minute. But now my problem is that, Suppose i had created timer for 5 minutes and when the remaining time is 3 and half minute and i submit that page, so at that time it will pass 4 minutes only because i pass it, so now the timer again started with 4 minutes. So it will create problem and the timer is again loaded with 4 minutes. so give me the solution. Thanking you. urvi

Chris Baron

Ranch Hand

Posts: 1061

posted 14 years ago

You could work with Math.floor( timeout/60 )instead of changing 1000 to 60000; But what happens in the last 59 seconds? Zero minutes were displayed while the last minute...rather irritating! I'd format the thing like a digital watch:

hi chris i found one bug in your code u have replied in jsp timer using javasctipt

all though it is working fine but when u give this timer a value 10 mins and 1 secs during initialization mean during the first call it will directly move it's value to the 00:58 why that so i can't figure it out is it is due to the parseInt() can u give me the answer [ November 03, 2004: Message edited by: ketu patel ]

ketu patel

Greenhorn

Posts: 7

posted 13 years ago

There is a "bug" with the parseInt JavaScript function. The bug is not something that will affect you very often, but it is something you should be aware of. We've seen the bug in every browser except Opera.

We've created a button to demonstrate the bug. The bug is that parseInt can return an incorrect value. For example, parseInt("08") results in 0 instead of 8. And parseInt("09") results in 0 instead of 9. The reason for this is because the zero in front is trying to tell the browser that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers. The button below builds statements from parseInt("01") through parseInt("09") and shows what the resulting value is. But it also does parseFloat("01") through parseFloat("09"). This shows that the bug does not exist with parseFloat.

Keep in mind that this bug only happens when the value being checked is a string and only when the string starts with a leading zero. So that's why it is difficult to notice. But if you're dealing with a web page that has user input, there's nothing prevening the user from entering 08 for a number field. To be 100% confident that you won't see the bug, use one of these two techniques:

Not possible. How do expect the timer to count down unless the page is refreshing itself every second? Which is clearly silly.
If someone disables Javascript, they don't use your site.
hth,
bear

does it mean that the timer won't work for those browsers with disabled javascript??? if that is true then people will deliberately disable javascript to remove the time constraint... what can be done to rectify that then...

Nothing. Once the page is sent to the browser, it's on its own. There's nothing the server-side can do to affect it any longer. And if JavaScript is disabled, then there's not much that can be done on the client either.

The only recourse is to refuse to display the page if JavaScript is disabled, and to keep info on the server (in the session, most likely) to detect if someone is refreshing the page to restart the timer.

To me, this problem has a simple obvious solution.
You have to store the "end time" for this 'exam' in the session on the server end.

When the user hits the "start exam now" button, You get the current time, add 30 minutes (or whatever your time limit for the test is) and then store that value to a session attribute.

When the user submits the exam, you compare the current server time to their stored time limit value

You will probably need a javascript countdown timer or similar on the JSP page. But this should be for visual reference on the client side only, and the server's time/limit should be the authoratitive one. Any time the page gets "reloaded" you work out the time remaining (based on the sesion attribute and current time) and set the javascript countdown timer appropriately.

The javascript timer should not get TOO far out of synch with the server. Particularly if the exam covers multiple pages, and the timer can keep getting synchronized with each request.
If they have javascript disabled on the page, then they don't get to see the useful countdown timer telling them how long they have to do the exam.
But that doesn't stop the clock.