Display countdown timer until given date using JavaScript from PHP

This function is used in my custom computer exam software to display a countdown timer that stops at the end of the exam. The timestamp is determined and stored in a file when the student clicks „begin exam”1) then it is resilient to page reloads since the destination timestamp never changes. Of course the timer is not exact since network latency is not taken into account, but this code is only used on a LAN 8-)

function jscd($ts) {
  $curts=time();
  $res="";
  $res.="<span id=cd></span>";
  $res.="<SCRIPT type='text/javascript' language='javascript'>
    var dst=(new Date().getTime()/1000) + $ts-$curts;
    function pitty() {
      var t=new Date().getTime()/1000;
      var diff=Math.floor(dst - t);
      var min=Math.floor(diff/60);
      var sec=diff%60;
      if(diff<0) {
        document.getElementById('cd').innerHTML = 'Lejárt az idő';
      } else {
        document.getElementById('cd').innerHTML = 'Hátralevő idő: ' + min + ' perc ' + sec + ' másodperc';
      }
      setTimeout('pitty()',100);
    }
    setTimeout('pitty()',100);";
  $res.="</SCRIPT>";
  return $res;
}
1)
code not included here

Linkbacks

Use the following URL for manually sending trackbacks: http://rigo.info/lib/plugins/linkback/exe/trackback.php/en:blog:display_countdown_timer_until_given_date_using_javascript_from_php
en/blog/display_countdown_timer_until_given_date_using_javascript_from_php.txt · Utolsó módosítás: 2009-04-14 00:00 (külső szerkesztés)
CC Attribution-Noncommercial-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0