javascript 每分钟将当前时间与变量进行比较

javascript compare current time to variable every minute

我在数据库中定义了一个开始日期,我需要知道开始日期晚于当前时间的时间。我试过使用 setInterval,但我无法访问 setInterval 函数之外的更新时间。

这是我需要将当前时间与数据库变量进行比较的内容:

if(startDate > now) {
 var status = 'pre';
}

我试过如下使用 setInterval,但我无法在 setInterval 函数之外访问更新的(当前时间)。

setInterval(function(){
  now = moment(new Date()).format();
  console.log('time1 ', now); // this works
}, 1000);

console.log('time2 ', now); // this doesn't

我做错了什么?

试试这个..

 var now = new Date();
 setInterval(function(){
    now = new Date();
 console.log('time1 ',   now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()); 
}, 1000);

 console.log('time2 ',   now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()); 
});

我最终使用 react-interval https://github.com/nkbt/react-interval 来完成这个。