所以我建立了一个网站,我试图在该网站上运行一个脚本来安排一个任务在每晚 12 点运行。因为我不能使用
require()
,因为我正在运行一个网络服务器,我正在使用browserify来使用node-cron任务。
我有
data_retrieve.js
,我正试图在我的 HTML 中实现它:
class RunCron {
constructor() {
var cron = require('node-cron');
cron.schedule('0 0 * * *', () => {
$.ajax({
type: "POST",
url: "/main.py",
data: { param: text}
}).done(function( o ) {
console.log("Done running py!");
});
});
//const cron = require('node-cron');
}
}
var r = new RunCron();
然后我有
bundle.js
正在生成,我每次都收到以下错误:
Uncaught TypeError: process.hrtime is not a function
at Scheduler.start (bundle.js:1791:33)
at new ScheduledTask (bundle.js:1749:29)
at createTask (bundle.js:1564:12)
at Object.schedule (bundle.js:1553:18)
at new RunCron (bundle.js:1229:8)
at 5.node-cron (bundle.js:1246:9)
at o (bundle.js:1:633)
at r (bundle.js:1:799)
at bundle.js:1:828
at bundle.js:1:318
跟随错误到
(bundle.js:1791:33)
:
1780 - 1821 行:
class Scheduler extends EventEmitter{
constructor(pattern, timezone, autorecover){
super();
this.timeMatcher = new TimeMatcher(pattern, timezone);
this.autorecover = autorecover;
}
start(){
// clear timeout if exists
this.stop();
let lastCheck = process.hrtime();
let lastExecution = this.timeMatcher.apply(new Date());
const matchTime = () => {
const delay = 1000;
const elapsedTime = process.hrtime(lastCheck);
const elapsedMs = (elapsedTime[0] * 1e9 + elapsedTime[1]) / 1e6;
const missedExecutions = Math.floor(elapsedMs / 1000);
for(let i = missedExecutions; i >= 0; i--){
const date = new Date(new Date().getTime() - i * 1000);
let date_tmp = this.timeMatcher.apply(date);
if(lastExecution.getTime() < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)){
this.emit('scheduled-time-matched', date_tmp);
date_tmp.setMilliseconds(0);
lastExecution = date_tmp;
}
}
lastCheck = process.hrtime();
this.timeout = setTimeout(matchTime, delay);
};
matchTime();
}
stop(){
if(this.timeout){
clearTimeout(this.timeout);
}
this.timeout = null;
}
}