javascript 将分钟添加到 new Date();在使用该对象之前

发布于 2024-12-27 11:48:43 字数 564 浏览 4 评论 0原文

我有一个函数调用启动了一堆计时器,我想为每个计时器添加几分钟,这样它们就不会全部从零开始。我取得了成功:

var currentDate = new Date();
var twentyMinutesLater = new Date(currentDate.getTime() + (20 * 60 * 1000));
new CountUp(twentyMinutesLater, 'counter03');

我很想跳过为我想要的所有计时器创建 var二十分钟后等等,我只是无法获得正确的语法,或者也许这是不可能的。有没有办法在下面的函数调用中添加毫秒。

new CountUp(new Date(), 'counter03');

我尝试过:

new CountUp((new Date() + (20 * 60 * 1000)), 'counter03');

结果 NaN NaN:NaN:NaN 所以它不是一个数字 带双引号的结果相同。

有没有 javascript 语法大师有什么想法?

I have a function calls that start a bunch of timers and I want to add a few minutes to each one so they don't all start off at zero. I had success with:

var currentDate = new Date();
var twentyMinutesLater = new Date(currentDate.getTime() + (20 * 60 * 1000));
new CountUp(twentyMinutesLater, 'counter03');

I would love to skip creating the var twentyMinutesLater and so on for all the timers I want, I just can't get the syntax right, or maybe it's not possible. Is there a way to add the milliseconds that in the function call below.

new CountUp(new Date(), 'counter03');

I've tried:

new CountUp((new Date() + (20 * 60 * 1000)), 'counter03');

Result NaN NaN:NaN:NaN so it's not a number
Same result with double quotes.

Any javascript syntax masters out there that have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亣腦蒛氧 2025-01-03 11:48:43

在您的特定代码中,您将 Date 对象传递给计数器代码,但这不是预期的结果。它需要一个时间值,从中生成自己的 Date 对象(您可以在 Counter 函数的构造函数中看到这一点)。另外,计数器代码不会占用未来的时间,只会占用过去的时间(也许是因为它不想处理负时间 - 我不知道)。

这是一个工作示例此处,我修改了在回答您的其他问题时使用的其他 jsFiddle问题。

function addCounter() {
    var currentDate = new Date();
    var twentyMinutesLater = currentDate.getTime() - (20 * 60 * 1000);
    var div = document.createElement("div");
    div.id = "counter" + counterNum++;
    document.body.appendChild(div);
    new CountUp(twentyMinutesLater, div.id);
}

而且,这里有一个 jsFiddle,可以让您输入分钟数,它将在该值启动一个计数器: http:// /jsfiddle.net/jfriend00/vnf5z/

In your specific code, you're passing a Date object to the counter code and that is not what it is expected. It is expecting a time value from which it will make it's own Date object (you can see that right in the constructor for the Counter function). Plus, the counter code won't take times in the future, only times in the past (perhaps because it doens't want to deal with negative times - I don't know).

Here's a working example here where I've modified my other jsFiddle used in the answer to your other question.

function addCounter() {
    var currentDate = new Date();
    var twentyMinutesLater = currentDate.getTime() - (20 * 60 * 1000);
    var div = document.createElement("div");
    div.id = "counter" + counterNum++;
    document.body.appendChild(div);
    new CountUp(twentyMinutesLater, div.id);
}

And, here's a jsFiddle that lets you enter a number of minutes and it will start a counter at that value: http://jsfiddle.net/jfriend00/vnf5z/.

单身情人 2025-01-03 11:48:43

应该执行类似以下操作:

var d = new Date();
new CountUp(d.setMinutes(d.getMinutes() + 20), 'counter03'); 

根据 CountUp 构造函数如何使用传递给它的日期对象,以及是否要重用 d,您可能需要:

var d = new Date();
new CountUp(new Date(d.setMinutes(d.getMinutes() + 20)), 'counter03'); 

以便每次调用 CountUp 获取不同的日期对象。

Something like the following should do:

var d = new Date();
new CountUp(d.setMinutes(d.getMinutes() + 20), 'counter03'); 

Depending on how the CountUp constructor uses the date object passed to it, and whether you want to re-use d, you might need:

var d = new Date();
new CountUp(new Date(d.setMinutes(d.getMinutes() + 20)), 'counter03'); 

so that each call to CountUp gets a different date object.

一笔一画续写前缘 2025-01-03 11:48:43

这是我在 Rails 中看到的另一种方法。您可以使用此语法创建相对日期对象,

(20).minutes().fromNow()

或者,如果您讨厌括号中的噪音,您可以这样做(仅在 ES5 兼容浏览器上),

(20).minutes.fromNow

这是第一个在 Number 原型上添加方法的非 ES5 解决方案。

Number.prototype.minutes = function() {
    // minutes to milliseconds
    return this * 60 * 1000;
};

Number.prototype.fromNow = function() {
    var futureDate = new Date(Date.now() + this);
    return futureDate;
};

new CountUp((20).mines().fromNow(), 'foo')

这是添加函数支持属性的 ES5 解决方案。

Object.defineProperties(Number.prototype, {
    minutes: {
        get: function() {
            // minutes to milliseconds
            return this * 60 * 1000;
        }
    },
    fromNow: {
        get: function() {
            var futureDate = new Date(Date.now() + this);
            return futureDate;
        }
    }
});

new CountUp((20).mines.fromNow, 'foo')

有关于扩展本地对象的不同思想流派。一组不惜一切代价禁止它,而另一组则鼓励几乎在任何地方使用它。与任何事情一样,取得平衡很重要。

Just another approach that I've seen been used in Rails. You'd create relative date objects using this syntax,

(20).minutes().fromNow()

Or, if you hate the noise from the parentheses, you could do this (only on ES5 compatible browsers),

(20).minutes.fromNow

Here's the first non-ES5 solution that adds methods on the Number prototype.

Number.prototype.minutes = function() {
    // minutes to milliseconds
    return this * 60 * 1000;
};

Number.prototype.fromNow = function() {
    var futureDate = new Date(Date.now() + this);
    return futureDate;
};

new CountUp((20).minutes().fromNow(), 'foo')

Here's the ES5 solution that adds function backed properties.

Object.defineProperties(Number.prototype, {
    minutes: {
        get: function() {
            // minutes to milliseconds
            return this * 60 * 1000;
        }
    },
    fromNow: {
        get: function() {
            var futureDate = new Date(Date.now() + this);
            return futureDate;
        }
    }
});

new CountUp((20).minutes.fromNow, 'foo')

There are different schools of thought on extending of native objects. One group forbids it at any cost, while the other encourage using it almost everywhere. As with anything, striking a balance is important.

复古式 2025-01-03 11:48:43

new Date() 确实返回一个对象而不是时间戳,因此您不应该在那里使用数学运算(当您期望结果为 Number 时,至少不要添加)。
使用 Date.getTime() :

//when you need a Date-object as argument
  new CountUp(new Date(new Date().getTime() + (20 * 60 * 1000)) , 'counter03');
//when you need the timestamp as argument
  new CountUp((new Date().getTime() + (20 * 60 * 1000)) , 'counter03');

请参阅小提琴以识别差异: http://jsfiddle.net/doktormolle/8v4Tx /

new Date() does return an object not the timestamp, so you should'nt use a mathematical operation there(at least no addition, when you expect a Number as result).
Use Date.getTime() :

//when you need a Date-object as argument
  new CountUp(new Date(new Date().getTime() + (20 * 60 * 1000)) , 'counter03');
//when you need the timestamp as argument
  new CountUp((new Date().getTime() + (20 * 60 * 1000)) , 'counter03');

See the fiddle to recognize the difference: http://jsfiddle.net/doktormolle/8v4Tx/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文