I am looking to enhance my JavaScript quiz counter by adding milliseconds. Currently, the quiz only tracks and displays time in seconds (stored in MySQL as a number of seconds, like 120 for 2 minutes displayed as 02:00). I now want to include milliseconds in the timer. Thank you in advance.
Below is the script:
<script type="text/javascript">
var quiz_timer = 0;
$(window).load(function(){
setInterval('run_timer()',1000 )
})
function run_timer()
{
quiz_timer++;
if(quiz_timer > 60)
{
sec = quiz_timer%60;
min = Math.floor(quiz_timer/60);
}
else
{
sec = quiz_timer;
min = 0;
}
var timer='';
if(min < 10)
timer = '0';
timer += min;
timer += ':';
if(sec < 10)
timer += '0';
timer += sec;
var timer_h = 'Time: '+timer;//+rand();
$('#quiz_timer').html(timer_h);
$('#quiz_time').val(quiz_timer);
}
function update_quiz()
{
var cnt_questions = parseInt($('#cnt_questions').val());
var cq = parseInt($('#current_question').val());
var op = $('#question_'+cq).find('input[type=radio]:checked').length;
if(op == 0)
{
alert('You must answer on the question.');
return false;
}
if(cq < cnt_questions)
{
$('#question_'+cq).hide();
$('#question_'+(cq+1)).fadeIn(1000);
$('#current_question').val(cq+1);
return false;
}
$(window).unbind('beforeunload');
document.frm_quiz.submit();
}
</script>
And here's the function that inputs the data into MySQL:
function timer($quiz_timer)
{
if($quiz_timer > 60)
{
$sec = $quiz_timer%60;
$min = floor($quiz_timer/60);
}
else
{
$sec = $quiz_timer;
$min = 0;
}
$timer='';
if($min < 10)
$timer = '0';
$timer .= $min;
$timer .= ':';
if($sec < 10)
$timer .= '0';
$timer .= $sec;
return $timer;
}