如何比较JavaScript中的日期? [英] How do I compare dates in Javascript?

查看:132
本文介绍了如何比较JavaScript中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我有一个函数运行一个循环,并且做的东西,错误的条件可能会使它退出该循环。
我想要检查循环是否仍在运行。

I have a certain function that runs a loop and does stuff, and error conditions may make it exit that loop. I want to be able to check whether the loop is still running or not.

为此,我在做,对于每个循环运行:

For this, i'm doing, for each loop run:

LastTimeIDidTheLoop = new Date();

另外在30秒内通过SetInterval运行的另一个函数中,我想基本上这样做: / p>

And in another function, which runs through SetInterval every 30 seconds, I want to do basically this:

if (LastTimeIDidTheLoop is more than 30 seconds ago) {
  alert("oops");
}

我该怎么做?

谢谢!

推荐答案

如何:

newDate = new Date()
newDate.setSeconds(newDate.getSeconds()-30);
if (newDate > LastTimeIDidTheLoop) {
  alert("oops");
}

这篇关于如何比较JavaScript中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆