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
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; }