我在JavaScript中有一个计时器,当我锁定手机时,计时器仍会计数几分钟,此后便停止。如果我解锁手机,计时器将开始计时。当手机屏幕关闭时,计时器是否还能计数?
<script>
var seconds=0;
function pad(val) { return val > 9 ? val : "0" + val; }
if(sessionStorage.getItem("self.totalSeconds") != null ){
$("#min").text(pad(Math.floor(sessionStorage.getItem("self.totalSeconds") / 60 % 60)));
$("#sec").text(pad(parseInt(sessionStorage.getItem("self.totalSeconds") % 60)));
seconds=parseInt(sessionStorage.getItem("self.totalSeconds"));
}else{
$("#min").text("00");
$("#sec").text("00");
}
var Clock = {
totalSeconds: seconds,
start: function () {
var self = this;
this.interval = setInterval(function () {
if(sessionStorage.getItem("self.totalSeconds") == null ){
self.totalSeconds += 1;
}else{
self.totalSeconds=parseInt(sessionStorage.getItem("self.totalSeconds"));
self.totalSeconds += 1;
}
sessionStorage.setItem("self.totalSeconds", self.totalSeconds);
$("#min").text(pad(Math.floor(sessionStorage.getItem("self.totalSeconds") / 60 % 60)));
$("#sec").text(pad(parseInt(sessionStorage.getItem("self.totalSeconds") % 60)));
}, 1000);
},
pause: function () {
clearInterval(this.interval);
delete this.interval;
},
};
Clock.start();
$('#pauseButton').click(function () {
$("#errorMessage").text("");
$("#errorMessage").hide();
var totalSecondstobeexported = Clock.totalSeconds;
});
</script>