案例: 自动签到的功能
第一步 安装依赖:
npm i node-schedule
第二步 创建一个node项目
index.js文件中演示使用的方法
import schedule from 'node-schedule'
import request from 'request' 调用接口用的
秒 分 时 日 月 星期 / 代表步长 每5秒执行一次
比如半夜12点30分钟就要写成 ('0 30 0 * * *')
schedule.scheduleJob('*/5 * * * * *', function(){
console.log('每隔5秒就会执行一次')
调用掘金签到的接口: 第一个参数掘金签到的地址:aid和uid可以通过登录掘金打开对应的
接口有的就存在。aid:2608,uuid:7196650628332422716,
request( 'https://api.juejin.cn/growth_api/v1/check_in?aid=你的aid&uid=你的uid‘,{
method:'POST',
headers:{
Referer:config.url, 这里是掘金的地址 https://juejin.cn/,
cookie:`sessionid=你的cookie`
}
},function(error,reponse,body){
console.log(body)
})
})
schedule.cancelJob('*/5 * * * * *', function(){
console.log(取消了定时任务)
} )