开发者

how to create a timer javascript component in a django template

开发者 https://www.devze.com 2023-03-05 21:27 出处:网络
im trying to create an online exam using django . i used a countdown javascri开发者_如何学Cpt timer to tell when the time is done.

im trying to create an online exam using django . i used a countdown javascri开发者_如何学Cpt timer to tell when the time is done.

but the problem is : each time i move to the next question page the timer restarts.

is there a way to make the time go on even after refreshing the page ?

here is my template code (which has the timer)

    <form name="counter"><input type="text" size="8" 
name="d2"></form> 

<script> 
<!-- 
// 
 var milisec=0; 
 var seconds={{time}}; 
 document.counter.d2.value={{time}}; 



function display()
{ 
 if (milisec<=0){ 
    milisec=9 
    seconds-=1 
 } 
 if (seconds<=-1){ 
    milisec=0 
    seconds+=1 
 } 
 else 
    milisec-=1 
    document.counter.d2.value=seconds+"."+milisec 
    setTimeout("display()",1000) 
} 
display() 
-->
</script>

thanks in advance


This is what I did.

You know when they started the test, and you know how long they have to take it, so when the test starts, calculate the expected end time, and store that somewhere (database, etc). Pass the end time as a variable to the template and use that in your javascript to caclulate the remaining time. Since the ending time will always be the same from page to page you don't have to worry about passing values between forms.

If you pass values between forms or set cookies then you are going to make it real easy for someone to hack the test to give themselves more time. Storing that value behind the scenes and not changing it on the pages will make a little harder to hack. They can still hack the javascript timer, but that is fine because after each question is submitted you will just check to see if current time is greater then end time, and if it is then you know time has expired and you can end the test.

If you use a jquery plugin like http://keith-wood.name/countdown.html it will make your life easier as well.

Hope that helps.


Use session to store the date\time.

You first create a variable if it does not exist, for instance:

import datetime
#Store the current time in session  
if 'time_started' not in request.session:   
    request.session['time_started'] = datetime.datetime.today()

To read the value you just use

time_started = request.session['time_started']

If you need to delete the time_started variable you can use this:

del request.session['time_started'] #delete time_started variable in session


The easiest to implement would be to just pass the current timer value in the POST parameters, and restart the timer using that timer value on the next page.

Its probably better to use ajax (possibly through a django library like dajax) seems like a good way to go, and have the timer on one page (which continuously updates the questions as they move along).

Also, if this online test is used in any way (e.g., as a quiz grade for a class), you should record the initial and final timestamp on the server and go by that difference only (possibly giving several extra seconds to account for communication lag). In general you can't trust that the javascript running hasn't been altered. It can still be there as a convenience to let them know how much time is left, but remember that all client side java-script is easily modified by the user to give themselves more time.


The easiest way in my mind, is to just use AJAX and replace the form in the current page. This way you don't have to refresh, and the timer will keep running. It'll also load the next form faster!

0

精彩评论

暂无评论...
验证码 换一张
取 消