有什么办法可以使我的分钟变化与momentjs循环吗?

时间:2019-05-05 19:25:37

标签: javascript jquery momentjs

我想使用momentjs在变化的那一刻找到偶数和奇数分钟。

我已经阅读了momentjs文档,并且通过一些搜索未找到我可以使用的任何东西或如何应用它。最终,我想使用jquery来使用addClass和removeClass每分钟更改导航栏的主题,而不是写入控制台。

var now = new Date();
  var minutes = now.getMinutes();
  if(minutes % 2 == 0){
    console.log('Time is even');
  } else {
    console.log("Time is odd");

3 个答案:

答案 0 :(得分:1)

您可以使用application/x-www-form-urlencoded函数以指定的间隔重复一些代码。示例:

setInterval

在您的情况下,您可能希望将时间间隔设置为1000 * 60,以便每分钟运行一次代码。请记住,setInterval函数不能保证精确到毫秒的间隔,但在大多数情况下已经足够了。

答案 1 :(得分:0)

我认为您宁愿使用setInterval

const intervalID = setInterval(function () {
   const minutes = now.getMinutes();
   if (minutes % 2 == 0) {
      console.log('Time is even');
   } else {
      console.log("Time is odd");
   }
}, 1000);

答案 2 :(得分:0)

每1秒钟(1000毫秒)检查一次条件,并获得奇/偶值

setInterval(function() {
  let now = new Date();
  let minutes = now.getSeconds(); // You can change getSeconds() to getMinutes()
  if(minutes % 2 == 0) {
    console.log('Time is even');
  } else {
    console.log("Time is odd");
  }
}, 1000)