javascript计时器和高分

时间:2017-04-16 00:19:06

标签: javascript html

您好我想使用秒表来计算我的html页面上的用户互动时间。一旦他们点击停止按钮,我想记录他们的高分时间,并在每次更好的时间发生时替换高分。我有我的秒表工作,但不知道如何找出高分部分。

我的html代码是:

     <body>
  <fieldset>
            <legend>Stop Watch</legend>
            <input type="button" id="start" value="Start">
            <input type="button" id="stop" value="Stop">
            <input type="button" id="reset" value="Reset">
            <span id="s_minutes">00</span>:
            <span id="s_seconds">00</span>:
            <span id="s_ms">000</span>
            <span id = "highscore"></span>
        </fieldset> 


parts of my javascript code is here:

"use strict";
var $ = function(id) { return document.getElementById(id); };

var stopwatchTimer;
var elapsed = { minutes:0, seconds:0, milliseconds:0 }; 


     var padSingleDigit = function(num) {
            return (num < 10) ? "0" + num : num;
        };

        var tickStopwatch = function() {    
            // increment milliseconds by 10 milliseconds
            elapsed.milliseconds = elapsed.milliseconds + 10;

            // if milliseconds total 1000, increment seconds by one and reset milliseconds to zero
            if (elapsed.milliseconds === 1000) {
                elapsed.seconds++;
                elapsed.milliseconds = 0;
            }
            // if seconds total 60, increment minutes by one and reset seconds to zero
            if (elapsed.seconds === 60) {
                elapsed.minutes++;
                elapsed.seconds = 0;
            }

            //display new stopwatch time
            $("s_minutes").firstChild.nodeValue = padSingleDigit(elapsed.minutes);
            $("s_seconds").firstChild.nodeValue = padSingleDigit(elapsed.seconds);
            $("s_ms").firstChild.nodeValue = elapsed.milliseconds;
        };

        // event handler functions
        var startStopwatch = function() {  

            tickStopwatch();
            stopwatchTimer = setInterval(tickStopwatch, 10);
        };

        var stopStopwatch = function() {
            // stop timer
            clearInterval(stopwatchTimer);
            var score ;
            var highscore=0;
           score = getTime();

            if (score>highscore){

            highscore== score;
            }
            $("highscore").firstChild.nodeValue = highscore;

        };

        var resetStopwatch = function() {
            // stop timer
            clearInterval(stopwatchTimer);

            // clear elapsed object and stopwatch display
            elapsed = { minutes:0, seconds:0, milliseconds:0 }; 

            $("s_minutes").firstChild.nodeValue = "00";
            $("s_seconds").firstChild.nodeValue = "00";
            $("s_ms").firstChild.nodeValue = "000";
        };

        window.onload = function() {


            $("start").onclick = startStopwatch;
            $("stop").onclick = stopStopwatch;
            $("reset").onclick = resetStopwatch;
        };

0 个答案:

没有答案