0
所以我有一点困难,找出一种方法让我的计时器在我的番茄钟计时器中每分钟重置为59秒。日期对象是否有一个内置的倒数计数方法?这里是我的代码:JavaScript日期对象重置
'use strict';
//Visual representation of work/break times and counter
let $workDisplay = $("#workNum"),
$breakDisplay = $("#breakNum"),
$workMinus = $(".workMinus"),
$workPlus = $(".workPlus"),
$breakMinus = $(".breakMinus"),
$breakPlus = $(".breakPlus"),
$counter = $("#counter");
//Initialize date object to work with functions (seven 0's to remove default local time)
let time = new Date(0, 0, 0, 0, 0, 0, 0);
let state = 'off';
//Set time dynamically
let setTime = function(minutes, seconds) {
minutes = time.setMinutes(minutes);
seconds = time.setSeconds(seconds);
return { minutes: minutes, seconds: seconds }
}
//Default value for minutes and seconds (25)
setTime(25 - 1, 59);
let getMinutes = time.getMinutes(setTime.minutes);
let getSeconds = time.getSeconds(setTime.seconds);
$workDisplay.text(getMinutes);
$counter.text(getMinutes);
//Timer states
let toggleTimer = function(newState) {
displayTime();
if (state !== 'done') {
//The ? serves as a toggle (statement ? true value : false value)
state = newState || (state == 'on' ? 'off' : 'on');
}
if (state == 'off') {
$counter.text(getMinutes + ":" + getSeconds);
}
console.log(state);
}
$counter.on('click',() => toggleTimer());
//Shrink these with an if statement later (check Dustin's DMs on Slack)
$workMinus.on('click', function() {
getMinutes--;
console.clear();
$workDisplay.text(getMinutes);
$counter.text(getMinutes);
console.log(getMinutes);
});
$workPlus.on('click', function() {
getMinutes++;
console.clear();
$workDisplay.text(getMinutes);
$counter.text(getMinutes);
console.log(getMinutes);
});
//Count down seconds and minutes
let countdownSecond = function() {
if (state == 'on') {
//Seconds stuff
getSeconds--;
$counter.text(getMinutes + ":" + getSeconds);
}
}
let countdownMinute = function() {
if (state == 'on') {
getMinutes--;
$counter.text(getMinutes + ":" + getSeconds);
}
}
//Update time display every minute/second
function displayTime() {
window.setInterval(countdownSecond, 1000);
window.setInterval(countdownMinute, 60000)
}
console.log(state);
目前,我的计时器下降到负面,而不是重新设置为59秒新的一分钟。事实上,它甚至从底片开始!任何提示,以帮助我的计时器正常运作,将不胜感激:)
你需要研究的Date对象。它的时间值表示自1970-01-01T00:00:00Z以来的毫秒数,设置'新日期(0,0,0,0,0,0)'创建日期为1899-12-31T00:00:00.000Z根据当地时区进行调整(日期对象将0解释为1900,第0天则为前一个月的最后一天)。 – RobG