我有为我的测验用户计算时间的脚本,现在我想知道如何在 mysql 中以毫秒为单位输入日期并稍后以这种方式显示,而不是仅以秒为单位(现在这样)。
这是我所拥有的:
我想知道如何将毫秒添加到我的 JS 测验计数器。此时,测验本身只计算并查找秒数(在 mysql 中是秒数,例如:120(2 分钟),显示为 02:00)。现在我想添加毫秒。提前致谢
这是脚本:
<script type="text/javascript">
var quiz_timer = 0;
var millisecondFactor = 60; //lesser this factor, accurate the timer will work
var sec = 0;
var min = 0;
var hour = 0;
$(window).load(function () {
setInterval('run_timer()', (1000 / millisecondFactor));
})
function run_timer() {
quiz_timer++;
millisec = quiz_timer;
if (millisec > millisecondFactor) {
sec++;
quiz_timer = 0;
}
if (sec > 59) {
min++;
sec = 0;
}
if (min > 59) {
hour++;
min = 0;
}
if (hour > 23) {
hour = 0;
}
var timer = '';
if (min < 10)
timer = '0';
timer += min;
timer += ':';
if (sec < 10)
timer += '0';
timer += sec;
timer += ':';
if (millisec < 10)
timer += '0';
timer += millisec;
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();
}
感谢 Vicky Gonsalves ^
这是在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;
}
这里还有显示测验时间的表格:
<?
require_once 'config.php';
isLoggedIn();
$page = 'top20';
$qry = 'select * from quiz where user_id="'.$_SESSION['USER_ID'].'" order by id desc limit 1';
$sql = $dbh->prepare($qry);
$sql->execute();
$c_quiz = $sql->fetch();
$qry = 'select a.user_id as userid,a.cnt_correct,a.quiz_time,a.id as q_id,b.* from users b left join quiz a on a.user_id = b.id where cnt_correct > 0
order by cnt_correct desc,quiz_time asc';
$sql = $dbh->prepare($qry);
$sql->execute();
$top = $sql->fetchAll();
$q_ids = array_keys($top);
$inc = 0;
$top20 = array();
foreach($top as $key=>$item)
{
if(array_key_exists($item['userid'],$top20))continue;
$inc++;
//$item = $item[0];
$top20[$item['userid']] = array($inc,$item['cnt_correct'],$item['q_id']);
}
foreach($top20 as $key=>$item){
if($c_quiz['id'] == $item[2])
{
$in_rating = true;
$top_place = $item[0];
$top_score = $item[1];
break;
}
}
//echo '<pre>';print_r($top20);
//if($in_rating)
if($action == 'quiz')
{
if($c_quiz['cnt_correct'] == 0)
setMessage('Niste odgovorili tačno ni na jedno pitanje');
//elseif($c_quiz['cnt_correct'] == 0)
//setMessage('Your last score is: 0');
else
setMessage('Imali ste ukupno:: '.$c_quiz['cnt_correct'].' tačnih odgovora. Nalazite se na: '.$top_place.' mestu');
}
//else
//setMessage('Score: '.$c_quiz['cnt_correct'].' Time taken: '.timer($c_quiz['quiz_time']));
//setMessage('Your last score is: '.$c_quiz['cnt_correct'].' Time taken: '.timer($c_quiz['quiz_time']));
//echo '<pre>';print_r($top);
require_once 'header.php';
?>
<div id="container">
<div class="content home top20">
<h2 class="animated">Top lista:</h2>
<div id="top20_table">
<table cellpadding=0 style="background-color:rgba(255,255,255,0.5);" width="750px">
<tr align="left">
<th width="70" align="left">Mesto</th>
<th width="200" align="left">Ime</th>
<th width="150" align="left">Rezultat</th>
<th width="100" align="left">Vreme</th>
</tr>
<?
$inc = 0;
$top20 = array();
foreach($top as $key=>$item){
if(in_array($item['userid'],$top20))continue;
$top20[] = $item['userid'];
$inc++;
if($inc == 20)break;
//$item = $item[0];
//print_r($item);die;
?>
<tr align="left">
<td><?=$inc?>.</td>
<td><?=$item['firstname'].' '.$item['lastname']?></td>
<td><?=$item['cnt_correct']?></td>
<td><?=timer($item['quiz_time'])?></td>
</tr>
<?
}
?>
</table>
</div>
</div>
</div>
<?
require_once 'footer.php';
?>