2016-10-11 99 views
0

我有一个在数据库中定义的开始日期,我需要知道开始日期大于当前时间。我试过使用setInterval,但我无法访问setInterval函数之外的更新时间。javascript将当前时间与每分钟变量进行比较

这是我所需要的当前时间来比较数据库变量:

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 

我在做什么错?

+0

范围错误,您应该在setInterval之外定义“now”。 – Carlos2W

+0

定义全局变量以获取外部值 –

+0

您能否显示使用此组件的组件? – tobiasandersen

回答

0

试试这个..

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()); 
}); 
+0

谢谢 - 但我得到了同样的结果 - 只有time1每秒更新一次 –

相关问题