0

我想知道如何将毫秒添加到我的 JS 测验计数器。此时,测验本身只计算并查找秒数(在 mysql 中是秒数,例如:120(2 分钟),显示为 02:00)。现在我想添加毫秒。提前致谢

这是脚本:

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

这是在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;
}
4

2 回答 2

1

试试这个:

<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();
    }
</script>
于 2013-10-11T10:55:08.870 回答
0

您当前的方式有一个非常重要的缺陷: setInterval 的时机并不完美。处理时间的更好方法是使用 Date 对象,获取开始时间,然后在 setInterval 中计算经过的时间。

这是一些示例代码,可以让您走上正轨。它只需要你按照你想要的方式格式化时间,你就可以开始了:

/* display the currently elapsed time */
function showTime(){
  var time = formatTime(Date.now() - startTime);
  document.getElementById('timer').innerHTML = time;
}

/* format the number of miliseconds how you wish them */
function formatTime(elapsed) {
  var hours, minutes, seconds, milis;

  hours = Math.floor(elapsed/(60*60*1000));
  elapsed -= hours * 60 * 60 * 1000;

  minutes = Math.floor(elapsed/(60*1000));
  elapsed += minutes * 60 * 1000;

  seconds = Math.floor(elapsed/1000);
  elapsed -= seconds * 1000;

  milis = elapsed;

  return 'H' + hours + ' M' + minutes + ' S' + seconds + ' F' + milis; // change this
}

/* start counting */
var startTime = Date.now();
/* runs every 10ms, but it's hardly noticeable */
var interval = window.setInterval(showTime, 10);

/* stop after a while */
window.setTimeout(function() { window.clearInterval(interval);}, 3000);

演示:http: //jsbin.com/EqoRezE/1/edit

PS: Date.now() 在您的特定目标浏览器上可能不可用。您可以在此处找到替代实现: https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

于 2013-10-11T10:18:09.437 回答