这里设置时间为1-10分钟内的随机时间,早上8点到晚上23点结束
//测试noide定时任务
var cron = require('cron');
var axios = require('axios');
var addMysql = require('../db/mysqlservice.js');
var dataList = [];
var dataListsocket;
//获取指定日期的次日上午8:00
function GetNextDate() {
var time = new Date()
//获取当前时间年月日
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var t=y+"-"+m+"-"+d+" "+"08:00:00";
var tDate = new Date(Date.parse(t.replace(/-/g, "/")));
tDate=+tDate+24*60*60*1000;
tDate = new Date(tDate);
return tDate;
}
Date.prototype.Format = function (fmt) { //author: zhengsh 2016-9-5
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var date = new Date(GetNextDate()).Format("yyyy-MM-dd hh:mm:ss")
startDate= date.replace(new RegExp("-","gm"),"/");
var startDateM = (new Date(startDate)).getTime(); //得到毫秒数
startDateM+=60*60*1000*24
console.info('startDateM',startDateM)
//判断第二天的时间
var flag=true;
setInterval(IntegralPointExecute,60*60*1000);
function IntegralPointExecute(){
var t=new Date()
var tdate = new Date(t).Format("yyyy-MM-dd hh:mm:ss")
var ts = (new Date(tdate)).getTime(); //得到毫秒数
if(ts>startDateM){
startDateM+=60*60*1000*24
flag=true;
addtimer()
querytimer()
}
}
//添加
const addtimer = (time) => {
setTimeout(() => {
let t=new Date()
console.log('开始时间',new Date(t).Format("yyyy-MM-dd hh:mm:ss"))
axios.post('http://localhost:9999/blockchain/invoke/add', {
})
.then(function (response) {
let t=new Date()
console.log('添加完成',new Date(t).Format("yyyy-MM-dd hh:mm:ss"));
})
var timedate=new Date().getHours()
console.info(('当前时间小时',timedate))
if(timedate<8||timedate>23){
flag=false
}
if(flag){
let t=new Date()
let delay = Math.floor(2+Math.random() * 9) * 1000 * 60
addtimer(delay)
console.log(delay,new Date(t).Format("yyyy-MM-dd hh:mm:ss"))
}
}, time)
}
addtimer()