0

私はクイズユーザーの時間をカウントするスクリプトを持っています.今、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();
}

ヴィッキー・ゴンサルベスに感謝^

そして、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';
?>
4

1 に答える 1

0

スクリプト全体を変更し、代わりに日付を使用しました。みんな、ありがとう。

于 2013-10-15T14:58:06.130 に答える