JS-在MySQL中添加计时器作为日期时间(以毫秒为单位) [英] JS - add timer in mysql as datetime with milliseconds

查看:74
本文介绍了JS-在MySQL中添加计时器作为日期时间(以毫秒为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有为我的测验用户计算时间的脚本,现在我想知道如何在mysql中以毫秒为单位输入日期,然后以这种方式显示它,而不是仅以秒显示(现在是这样)。

i have the script that count the time for my quiz users, now i would like to know how can i input the date in mysql as milliseconds and display it later that way, instead of seconds only (as it is now.).

这就是我所拥有的:

我想知道如何为我的JS添加毫秒测验柜台。此时,测验本身仅计算并寻找秒数(在mysql中为秒数,例如:120(2分钟),显示为02:00)。现在我想增加毫秒。预先感谢

I would like to know how can i add milliseconds to my JS counter of quiz. At this point the quiz itself only count and look for seconds (in mysql it's a number of seconds, like: 120 (2 minutes), and it's displayed as 02:00). Now i would like to add milliseconds to it. Thanks in advance

以下是脚本:

<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 ^

Thanks to Vicky Gonsalves ^

这是在mysql中输入数据的函数:

And here's the function that input the data in 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;
}

此外,这是显示测验时间的表格:

Also here's the table that display the quiz time:

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


推荐答案

我更改了整个脚本,并改用了日期。谢谢大家。

I have changed whole script and used the date instead. Thanks everyone.

这篇关于JS-在MySQL中添加计时器作为日期时间(以毫秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆