javascript - 尝试在 JS 中运行此代码。

标签 javascript html

目标是创建一个网页来计算用户访问该网页的时间。我的方法是使用秒表来显示用户在页面上的时间。我还使用了 window.onblur() 函数来查看用户是否位于同一网页上。这两个代码本身运行得很好。我只是无法将它们制作成一个程序。

Stopwatch program: 
<!DOCTYPE html>
<html>
<body>
<h1 font size= 55 align= "center"><time>00:00:00</time></h1>
<h2 id= "mee" align= "center"></h2>
<h3  id="she" align= "center"></h3>
<button id="start" class= "fof"align= "center">start</button>
<button id="stop" align= "center">stop</button>
<button id="clear" align= "center">clear</button>
<style>
.fof{ height: 50px;
border-radius: 50px;
}
</style>
<script>
var h1 = document.getElementsByTagName('h1')[0],
start = document.getElementById('start'),
stop = document.getElementById('stop'),
clear = document.getElementById('clear'),
seconds = 0, minutes = 0, hours = 0,
t;




function add() {
    seconds++;
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        if (minutes >= 60) {
            minutes = 0;
            hours++;
    }
}

    h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00")         
+ ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00")
 + ":" +   (seconds > 9 ? seconds : "0" + seconds);

timer();

}


function timer() {
t = setTimeout(add, 1000);
}
timer();


/* Start button */
start.onclick = timer;



/* Stop button */
stop.onclick = function() {
clearTimeout(t);
    document.getElementById("mee").innerHTML= minutes;
    document.getElementById("she").innerHTML= seconds;

}

/* Clear button */
clear.onclick = function() {
h1.textContent = "00:00:00";
seconds = 0; minutes = 0; hours = 0;
}




</script>
</body>
</html>

Program to see if user is on same webpage:
    var quitter = false;
window.onblur = function () {

  if(!quitter){
        quitter = true;
        alert('You left');

    }

最佳答案

检查下面的 fiddle

https://jsfiddle.net/yogesh078/5vfxk9e1/1/

Javascript代码:

var h1 = document.getElementsByTagName('h1')[0],
    start = document.getElementById('start'),
    stop = document.getElementById('stop'),
    clear = document.getElementById('clear'),
    seconds = 0,
    minutes = 0,
    hours = 0,
    t;
var timerRunning = false;



function add() {
    seconds++;
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        if (minutes >= 60) {
            minutes = 0;
            hours++;
        }
    }

    h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") +
        ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") +
        ":" + (seconds > 9 ? seconds : "0" + seconds);

    timer();

}


function timer() {
    if (timerRunning) {
        clearTimeout(t);
    }
    t = setTimeout(add, 1000);
    timerRunning = true;
}
timer();


/* Start button */
start.onclick = timer;



/* Stop button */
stop.onclick = function() {
    clearTimeout(t);
    timerRunning = false;
    document.getElementById("mee").innerHTML = minutes;
    document.getElementById("she").innerHTML = seconds;

}

/* Clear button */
clear.onclick = function() {
    h1.textContent = "00:00:00";
    seconds = 0;
    minutes = 0;
    hours = 0;
}




var quitter = false;
window.onblur = function() {
    clearTimeout(t);
    timerRunning = false;
    quitter = true;
    alert('You left');
}

window.onmousemove = function() {
    if (!timerRunning) {
        timer();
    }
};

关于javascript - 尝试在 JS 中运行此代码。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645279/

相关文章:

javascript - 在 Webkit 之外寻找 Javascript 引擎

jquery - 用CSS类加载图像?

javascript - 根据加载文件的内容阻止分配的成功处理程序

javascript - 在输入完成最大长度后防止不必要的光标

html - 启用时更改复选框颜色

html - 链接到列表中单个元素的心脏/喜欢按钮

html - 在div底部而不是div顶部显示框

javascript - 年龄验证弹出窗口仅显示一次

Javascript - 范围问题和将参数传递给动态创建的事件处理程序

javascript - 是否可能/如何像 Google 插件一样设置 jQuery 自动完成的延迟?