dongyue5686 2019-04-27 13:09
浏览 106

如何在HTML中插入javascript时间变量

I have a Stopwatch timer source code that I am trying to modify.

Eventually, I will have the resulting time to insert the value into a mySQL table, however, I am struggling to retrieved the raw MS elapsed time from the JS code.

I would also like to define it into a PHP Variable called $elapsed (example), so that I can then insert this into Mysql

I have tried various methods and different variables from teh JS however I am a bit new to JS.

If I use 'ms' then it will just show the current ms value, not the total elapsed time in ms.

var clsStopwatch = function() {
        // Private vars
        var startAt = 0;    // Time of last start / resume. (0 if not running)
        var lapTime = 0;    // Time on the clock when last stopped in milliseconds
        //var elapsed = 0; //current elapsed time in ms

        var now = function() {
                return (new Date()).getTime(); 
            }; 

        // Public methods
        // Start or resume
        this.start = function() {
                startAt = startAt ? startAt : now();
            };

        // Stop or pause
        this.stop = function() {
                // If running, update elapsed time otherwise keep it
                lapTime = startAt ? lapTime + now() - startAt : lapTime;
                startAt = 0; // Paused
            };

        // Reset
        this.reset = function() {
                lapTime = startAt = 0;
            };

        // Duration
        this.time = function() {
                return lapTime + (startAt ? now() - startAt : 0); 
            };
    };

var x = new clsStopwatch();
var $time;
var clocktimer;

function pad(num, size) {
    var s = "0000" + num;
    return s.substr(s.length - size);
}

function formatTime(time) {
    var h = m = s = ms = 0;
    var newTime = '';

    h = Math.floor( time / (60 * 60 * 1000) );
    time = time % (60 * 60 * 1000);
    m = Math.floor( time / (60 * 1000) );
    time = time % (60 * 1000);
    s = Math.floor( time / 1000 );
    ms = time % 1000;

    newTime = pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3); //without hour
    //newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3); //with hour
    return newTime;
}

function show() {
    $time = document.getElementById('time');
    update();
}

   function update() {
    $time.innerHTML = formatTime(x.time());
  document.getElementById('elapsed').innerHTML = ms;


}   




function start() {
    clocktimer = setInterval("update()", 1);
    x.start();
}

function stop() {
    x.stop();
    clearInterval(clocktimer);
}

function reset() {
    stop();
    x.reset();
    update();
}
var pressed = true;
document.body.onkeyup = function(e) {
    if(e.keyCode == 32){
      if(pressed) {
        start();
        pressed = false;
      } else { 
        stop();
        pressed = true; 
      }
    }
    if(e.keyCode == 82) {
      reset()
     }
}
// This is then referenced to in the HTML Span

<div><span id="elapsed"></span>&nbsp;ms</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题