最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - how to create a timer javascript component in a django template - Stack Overflow

programmeradmin1浏览0评论

im trying to create an online exam using django . i used a countdown javascript 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

im trying to create an online exam using django . i used a countdown javascript 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

Share Improve this question asked May 15, 2011 at 19:35 Moayyad YaghiMoayyad Yaghi 3,73215 gold badges55 silver badges68 bronze badges 1
  • You could use Javascript to set a cookie, but if this is for an online exam, that wouldn't work well at all. When the person goes onto the next page, does the user click a button and send a form? You could send the value through POST parameters and pick it up on the next page. – tlunter Commented May 15, 2011 at 19:38
Add a ment  | 

4 Answers 4

Reset to default 5

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 munication 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)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>