0

我正在用 Javascript 编写番茄钟。

我对DateObj 有点困惑。根据下面的代码,想法是“会话计时器”和“中断计时器”值将存储在全局 obj 中,以便可以检索和/或更新它们。但是,以下内容让我感到困惑:

// The following stores the current time
// which makes sense.
const meter = {
    break : new Date(),
    session : new Date()
};
// However, when I set the minutes on the Date obj
// the above break & session props are no longer
// a Date obj, instead they are number.
// So, I cant use the 'getMinutes()'
const meter = {
    break : new Date().setMinutes( 5 ), // is a number, not Date anymore!
    session : new Date().setMinutes( 10 ) // is a number, not Date anymore!
};

我可以通过new Date()方法传递休息/会话编号以创建新日期,但我想知道如何创建计时器(主要格式为 [hr]:[min]:[sec])。在更新代码之前,我是否必须每秒执行以下操作:

let number = 1470650701308; // time in milliseconds as returned by methods (getMinutes(), getHours(), getSeconds())
let hours = 1470650701308 / ( 1000 * 60 * 60 ); // ms * sec * min 
let minutes = 1470650701308 / ( 1000 * 60 ); // ms * sec
let seconds = 1470650701308 / ( 1000 ); // ms

所以我的问题是如何以小时/分钟和秒为单位获取时间,以便我可以每秒更新时钟?

谢谢

4

1 回答 1

0

setminutes 方法修改对象并返回一个 int。

试试这个:

const meter = {
    break : new Date(),
    session : new Date()
};

meter.break.setMinutes(5);
meter.session.setMinutes(10);
于 2016-08-08T11:00:51.960 回答