时间Date和超时逾期设定
本帖最后由 pidtfork 于 2020-10-28 18:27 编辑时间 Date()和超时逾期设定
Date对象是JavaScript的内置对象,用于处理日期和时间。
通过new 方法构造,返回一个Date对象,实例化时未传入参数时,自动把当前日期和时间保存为其初始值。也可以指定初始值,参数可以是1970 年 1 月 1 日至今的毫秒数,也可以是如下固定格式的字符串。
var t = new Date("2020/4/2113:58:00");
vartm = new Date("2020/4/2113:58:00").getTime();
逾期设定
项目中很多时候需要一个时间节点的通知,如项目实施交付3个月后可以Pad弹窗提醒客户注意机房卫生啊,定期检查设备运行和功能状态,这样可以提供一些人性化的服务。
可以通过如下方法设定一个时间,定时的获取当前时间去比较设定的时间,超过时间就触发某些事件。
时间转成毫秒数是为了比较方便
var overdueTime = new Date("2020/4/21 13:58:00").getTime();//逾期时间
var now = new Date().getTime(); //当前时间
实例
逾期构造函数,可以实例化多个逾期事件。
可以设置检查逾期的时间间隔,默认1秒,也可以设置检查多少次后才触发一个事件回调;避免逾期后频繁触发回调事件。
//"2020/4/2113:58:00"
function overdueEvent(dates,callback) {
this.stopTime = new Date(dates).getTime();
this.callback =callback;
this.state = false;
this.init = function(){
varlastTime = getSystemInt("time" + this.stopTime,-1);
varnowTime = new Date().getTime();
if(lastTime > nowTime) {
this.state = true;
}else{
setSystemInt("time" + this.stopTime,nowTime)
}
}
this.intervalTime = 1;//间隔 n 秒检查时间是否逾期
this.intervalCall = 2;//m此检查后触发回调事件 每次触发回调事件为 n*m
this.intervalCount = 0; //计数
varself = this;
delay(this.intervalTime,function(){
setText("A1",new Date().getTime());
if(new Date().getTime() > self.stopTime){
self.state = true;
}
if(self.state){
self.intervalCount++;
if (self.intervalCount >= self.intervalCall) {
self.intervalCount = 0;
self.callback();
}
}
delay(self.intervalTime,arguments.callee);
});
}
// 设定的时间,固定为此格式2.回调,逾期触发此回调
varstops = new overdueEvent("2020/4/2113:58:00",function(){
setText("B1","overdueEvent");
setPageShow("弹窗");
setSystemReboot(); //重启设备
});
stops.init(); //设备上电运行 检查上次开机时间和本次开机时间
//主要作用是防止设备长时间断电导致时间错误,或人为将事件往前设置
页:
[1]