javaScript(js)⼿写原⽣任务定时器源码
javaScript(js)⼿写原⽣任务定时器
功能介绍
定时器顾名思义就是在某个特定的时间去执⾏⼀些任务,现代的应⽤程序早已不是以前的那些由简单的增删改查拼凑⽽成的程序了,⾼复杂性早已是标配,⽽任务的定时调度与执⾏也是对程序的基本要求了。通过时间表达式来进⾏调度和执⾏的⼀类任务被称为定时任务,很多业务需求的实现都离不开定时任务。
在javascript中要实现定时任务也是很简单的,可以选择插件,也可以⾃⼰写⼀个简单的定时任务,这⾥就个给⼤家写⼀个简单的setInterval() 循环定时任务。功能有,启动定时任务、停⽌定任务、添加定时任务、清除定时任务、执⾏定时器的⽅法。
1.定义两个⽅法有参和⽆参
function a1(value = "") {
原生js和js的区别console.log("定时任务被调⽤了携带的参数==>>", value)
}
function a2() {
console.log("定时任务被调⽤了我是⽆参数")
}
2. 实现定时任务
let daily_time = {
func: a1,
parm: "我是每秒",
year: "*",
month: "*",
date: "*",
hours: "*",
minutes: "*",
seconds: "*"
}
let monthly_transaction = {
func: a2,
parm: null,
year: "*",
month: "*",
date: "*",
hours: "*",
minutes: "1",
seconds: "*"
}
let monthly_summary = {
func: a1,
parm: "我是每⼩时 1分 1秒",
year: "*",
month: "*",
date: "*",
hours: "*",
minutes: "1",
seconds: "1"
}
addTimer(daily_time)
addTimer(monthly_transaction)
addTimer(monthly_summary)
startTimer()
创建定时任务js 代码⽂件
bg-timer.js
// 缓存定时任务
// {
//    func ⽅法名
//  parm ⽅法参数
//    year 年
//    month ⽉
//  date ⽇
//    hours 时
//    minutes 分
//    seconds 秒
/
/ }
//  * 表⽰每刻都执⾏数字表⽰定时这个时间执⾏/**
* 每年 1⽉1⽇1时1分1秒
* year *
* month 1
* date 1
* hours 1
* minutes 1
* seconds 1
*/
/**
* 每年每⽉每⽇每时1分每秒
* year *
* month *
* date *
* hours *
* minutes 1
* seconds *
*/
/**
* 每秒
* year *
* month *
* date *
* hours *
* minutes *
* seconds *
*/
var timer_list = []
var is_timer = null
// 启动
function startTimer() {
console.log("启动定时任务")
if (!is_timer) {
timeoutFunc()
}
}
// 停⽌
function stopTimer() {
console.log("停⽌定时任务")
if (is_timer) {
clearInterval(is_timer);
is_timer = null
}
}
// 清除定时任务
function cleanTimer() {
stopTimer()
timer_list = []
}
// 添加定时任务
function addTimer(item = {}) {
let n = {
"func": item.func || null,
"parm": item.parm || null,
"year": ar || "*",
"month": h || "*",
"date": item.date || "*",
"hours": item.hours || "*",
"minutes": item.minutes || "*",
"seconds": item.seconds || "*"
}
timer_list.push(n)
}
function timeoutFunc() {
if (is_timer) {
return
}
is_timer = setInterval(function() {
let da = new Date()
let fullYear = (da.getFullYear()).toString()
let month = (da.getMonth()).toString()
let dat = (da.getDate()).toString()
let hours = (da.getHours()).toString()
let minutes = (da.getMinutes()).toString()
let seconds = (da.getSeconds()).toString()
// console.log("定时......",timer_list)
/
/ console.log("年:", fullYear)
// console.log("⽉:", month)
// console.log("⽇:", dat)
// console.log("时:", hours)
// console.log("分:", minutes)
// console.log("秒:", seconds)
for (let i in timer_list) {
let item = timer_list[i]
if (ar != "*" && ar != fullYear) {
continue
}
if (h != "*" && h != month) {
continue
}
if (item.date != "*" && item.date != dat) {
continue
}
if (item.hours != "*" && item.hours != hours) {
continue
}
if (item.minutes != "*" && item.minutes != minutes) {
continue
}
if (item.seconds != "*" && item.seconds != seconds) {
continue
}
console.log("调⽤定时任务", item)
if (item.func && item.parm) {
item.func(item.parm)
} else {
item.func()
}
}
}, 1000)
}
结束:
  感谢个朋友们的⽀持,如有喜欢请点赞评论
现在最简单的定时任务已经完成了,各位朋友可以从此基础去往更深层次的定时任务进⾏下⼀步的研发和修改。也希望各位朋友把研发结果分析在评论区。
点个赞呗!