JavaScript 计时器不会在指定时间恢复
JavaScript timer doesn't resume at specified time
我得到了我不久前为自己写的计时器。昨天我实现了暂停功能,但是当我暂停它时,记录暂停时间并尝试再次将其传递给新的计时器 - 它比我暂停它更早开始。我整天都在挖掘,如何确定问题出在哪里。
请帮忙。
附言忽略/1000
,我需要Unix时间戳用于不同的目的=)
var timerId;
function startTimer(started) {
var localTime = new Date()/1000;
return setInterval(function() {
var totalSeconds = ((new Date()/1000) - started);
var hours = Math.floor(totalSeconds / 3600);
totalSeconds = totalSeconds % 3600;
var minutes = Math.floor(totalSeconds / 60);
totalSeconds = totalSeconds % 60;
var seconds = Math.floor(totalSeconds);
hours = (hours < 10 ? "0" : "") + hours;
minutes = (minutes < 10 ? "0" : "") + minutes;
seconds = (seconds < 10 ? "0" : "") + seconds;
var timeString = hours + ":" + minutes + ":" + seconds;
$('.timer .digits').text(timeString);
}, 1000);
}
$('.start button').click(function() {
timerId = startTimer(new Date()/1000);
$('.start').hide().siblings('.pause').show();
});
$('.pause button').click(function() {
var pausedTime = new Date()/1000;
clearInterval(timerId);
$('.timer').data('pausedTime', pausedTime);
$('.pause').hide().siblings('.resume').show();
});
$('.resume button').click(function() {
var pausedTime = $('.timer').data('pausedTime');
timerId = startTimer(pausedTime);
$('.resume').hide().siblings('.pause').show();
});
.pause, .resume {
display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='timer'>
<div class='digits'>00:00:00</div>
<div class='start'><button>Start</button></div>
<div class='pause'><button>Pause</button></div>
<div class='resume'><button>Resume</button></div>
</div>
希望你正在寻找这样的东西。由于这是一个计时器,您不需要特定的时间,只需保留经过的时间并在恢复时获取它。
var timerId;
function startTimer() {
var elapsed = ($('.timer').data('pausedTime') | 0) || 0;
return setInterval(function() {
var totalSeconds = elapsed;
var hours = Math.floor(totalSeconds / 3600);
totalSeconds = totalSeconds % 3600;
var minutes = Math.floor(totalSeconds / 60);
totalSeconds = totalSeconds % 60;
var seconds = Math.floor(totalSeconds);
hours = (hours < 10 ? "0" : "") + hours;
minutes = (minutes < 10 ? "0" : "") + minutes;
seconds = (seconds < 10 ? "0" : "") + seconds;
var timeString = hours + ":" + minutes + ":" + seconds;
$('.timer .digits').text(timeString);
$('.timer').data('pausedTime', ++elapsed);
}, 1000);
}
$('.start button').click(function() {
timerId = startTimer();
$('.start').hide().siblings('.pause').show();
});
$('.pause button').click(function() {
clearInterval(timerId);
$('.pause').hide().siblings('.resume').show();
});
$('.resume button').click(function() {
timerId = startTimer();
$('.resume').hide().siblings('.pause').show();
});
.pause, .resume {
display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='timer'>
<div class='digits'>00:00:00</div>
<div class='start'><button>Start</button></div>
<div class='pause'><button>Pause</button></div>
<div class='resume'><button>Resume</button></div>
</div>
相关文章:
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- 如何临时暂停浏览器渲染,然后恢复整个页面
- 绑定时将Parsley minlength消息作为选项传递时,未对其进行自定义
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 谷歌地图劫持了iphone's滚动(触摸事件)-如何恢复
- 我希望在不替换现有变量的情况下恢复localStorage中的变量
- 克隆和恢复”;工具化的“;元素
- 如何暂停和恢复jquery间隔
- 定时Ajax进程在用户离开页面后继续
- 将菜单项与滚动绑定时出现Jquery错误
- 定时循环,使用Javascript在2个循环之间间隔x秒
- 定时循环,间隔10秒
- 如何在php/javascript中制作定时器和警报
- AngularJS依赖注入定时问题
- 在python中,我如何才能恢复被隐藏的元素的内容
- Jquery定时问题
- 移动定时触摸事件
- 在尝试恢复暂停(停止)功能时卡住-番茄定时器
- javascript倒计时定时器暂停恢复
- 如何恢复定时器