This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes how to create timer in jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to create timer in jsp?" Watch "how to create timer in jsp?" New topic
Author

how to create timer in jsp?

urvi patel
Greenhorn

Joined: May 30, 2003
Posts: 17
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
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

Joined: May 30, 2003
Posts: 17
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

Joined: May 30, 2003
Posts: 17
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 ]


Thank you..
For replying me...
Ashish Kothari
Ranch Hand

Joined: May 16, 2003
Posts: 37
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

Joined: Mar 21, 2003
Posts: 1049
@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

Joined: May 30, 2003
Posts: 17
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

t try to do it with pure jsp would be better approach.

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
Ashish Kothari
Ranch Hand

Joined: May 16, 2003
Posts: 37
Urvi:
I also never tried this approach ,this is just a suggestion .
I was asked in interview ,what if javascript is disabled?then what to do?
Ashish
urvi patel
Greenhorn

Joined: May 30, 2003
Posts: 17
Originally posted by Ashish Kothari:
Urvi:
I also never tried this approach ,this is just a suggestion .
I was asked in interview ,what if javascript is disabled?then what to do?
Ashish

Hi..
It's ok... I just asked..
[ June 02, 2003: Message edited by: urvi23 patel ]
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
@Ashisch
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

Joined: May 30, 2003
Posts: 17
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

Joined: Mar 21, 2003
Posts: 1049
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:


Edit: Changed parseInt( mins ) to parseInt( mins, 10 )
thx for the PM, Ketu
[ November 04, 2004: Message edited by: Chris Baron ]
ketu patel
Greenhorn

Joined: Oct 16, 2004
Posts: 7
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

Joined: Oct 16, 2004
Posts: 7
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:

parseInt(parseFloat(<my text value> )

parseInt(<my text value>, 10)
makk mukhi
Greenhorn

Joined: Feb 22, 2011
Posts: 3
Bear Bibeault wrote:
t try to do it with pure jsp would be better approach.

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...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
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.

tick...tick...tick....

makk mukhi
Greenhorn

Joined: Feb 22, 2011
Posts: 3
hey thank you ... that was really helpful..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create timer in jsp?
 
Similar Threads
timer in jsp
Disabling Refresh Button
how to create timer in jsp
how to create timer in jsp page
how to create timer in jsp page ?