废话不多说,直接上代码
var fs=require('fs');
var schedule = require("node-schedule");
var http = require('http');
var content = document.getElementById('content');
var button = document.getElementById('button');
var api_url = document.getElementById("api_url")
var tips = document.getElementById("tips")
const { net } = require('electron').remote
var back_path = document.getElementById('back_path');
button.onclick = () =>{
if (api_url.value == '') {
alert("请输入接口地址") ;
return false;
}
httpGet(api_url.value)
getCallback(back_path.value);
/*fs.readFile('package.json','utf8',function(err,data){
console.log(data);
content.innerHTML = data
});*/
}
function getCallback(backPath) {
schedule.scheduleJob('*/5 * * * * *', function(){
console.log(backPath)
});
}
function httpGet(url){
schedule.scheduleJob('*/3 * * * * *', function(){
console.log('scheduleCronstyle:' + new Date());
const request = net.request(url)
request.on('response', (response) => {
//console.log(`STATUS: ${response.statusCode}`)
//console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
response.on('data', (chunk) => {
var data = JSON.parse(chunk)
console.log(data)
if (data.code == '200') {
} else {
var pNode=document.createElement("p");
pNode.innerHTML = "<p>"+data.msg+'</p>'
tips.appendChild(pNode)
}
})
response.on('end', () => {
console.log('No more data in response.')
})
})
request.end()
});
}
httpGet 方法就是定期器执行,之前是放在click 里面,但是不能执行,只能新建方法就可以了