我有应用程序,每当它第一次发布时,我应该执行一些逻辑。我已经检查了谷歌,但我没有得到任何良好的工作逻辑, 如果有人知道这一点,请建议我。
到目前为止我已尝试使用此代码
View2
答案 0 :(得分:2)
请注意:
var firstTime = new Date().getTime();
if(window.localStorage.getItem('firstTime') == null){
window.localStorage.setItem('firstTime', firstTime);
您正在存储一个代表当前时刻的数字到毫秒。如果您想要当天的开始,请先将小时数设为零:
var firstTime = new Date().setHours(0,0,0,0);
现在做逻辑,存储时间值很好:
if (window.localStorage.getItem('firstTime') == null){
window.localStorage.setItem('firstTime', firstTime);
在else部分,您需要与当天开始时的另一个时间值进行比较,如上所述:
} else {
var storedTime = window.localStorage.getItem('firstTime');
// Get a new date, zero it as above and see if its the same time
// If not, it must be a different day
var secondTime = new Date().setHours(0,0,0,0)
if (secondTime != +firstTime) {
alert("Second time is a different day");
} else {
alert("First time is the same day");
}
}
答案 1 :(得分:0)
不是存储var win = this.getRfRWindow();
var form = win.down('form');
var startTime = win.down('form').down('timefield[name=sTime]');
var endTime = win.down('form').down('timefield[name=eTime]');
endTime.clearValue();
endTime.setMinValue(startTime.getValue());
,而是存储new Date().getTime();
并进行比较。如果当前日期大于存储日期,则该应用程序将在当天首次打开。所以你存储这个日期。当下次应用程序打开时,当前日期不会更大(它会相等),因此您将理解这不是第一次。
注意: new Date().getDate();
只会为您提供日期的天部分。例如,对于今天的日期,即2016年3月10日,它将给你“10”。所以请包括几个月以使你的逻辑变得万无一失。
答案 2 :(得分:0)
这会像魅力一样 感谢@camelcasecoder
var firstTime = new Date().setHours(0,0,0,0);
alert("firstTime "+firstTime);
if (window.localStorage.getItem('firstLaunch') == null){
window.localStorage.setItem('firstLaunch', firstTime);
alert("At first time");
} else {
var storedTime = window.localStorage.getItem('firstLaunch');
// Get a new date, zero it as above and see if its the same time
// If not, it must be a different day
var secondTime = new Date().setHours(0,0,0,0)
alert("secondTime "+secondTime);
if (secondTime > storedTime) {
alert("First time in the day");
window.localStorage.setItem('firstLaunch', secondTime);
} else {
alert("Second time in the same day");
}
}