2013-10-11 1 views
0

퀴즈 사용자 시간을 계산하는 스크립트가 있습니다. 이제는 mysql에서 밀리 초 단위로 날짜를 입력하고 나중에 그 방법으로 표시하는 방법을 알고 싶습니다. 초만 (현재 그대로).JS - MySQL의 타이머를 밀리 초로 datetime으로 추가

내가 퀴즈 내 JS 카운터에 밀리 초를 추가하는 방법을 알고 싶습니다 : 여기

내가 가진 것입니다. 이 시점에서 퀴즈 자체는 단지 초를 세고 찾는다. (mysql에서 그것은 120 초 (2 분)와 같이 초 단위의 숫자이며 02 시로 표시된다). 이제는 밀리 초를 추가하고 싶습니다.^

비키 Gonsalves에

<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'; 
?> 
+0

꽤 혼란 스럽네요 ... 여기는 이해가 안됩니다. PHP 서버에서 타이머를 실행하려고합니까? –

+0

예, PHP 서버 Vicky에서 실행 중입니다 –

+0

어 .. PHP 스크립트가 url PHP 스크립트가로드를 시작하고 타임 스탬프를 캡처 할 때 타임 스탬프를 캡처해야합니다. 양식 및 당신 timeduration 줄 것이다 차이를 가져 가라. 아니면 당신은 자바 스크립트에서 PHP 서버로 시간을 직접 제출할 수 있습니다 ... –

답변

0

전체 스크립트를 변경하고 대신 날짜를 사용했습니다. 모두에게 감사드립니다.

관련 문제