加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

用JS编辑任务定时器的过程和代码是什么

发布时间:2023-08-04 13:00:12 所属栏目:语言 来源:
导读:关于“用JS写任务定时器的过程和代码是什么”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就

关于“用JS写任务定时器的过程和代码是什么”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。

功能介绍

定时器顾名思义就是在某个特定的时间去执行一些任务,现代的应用程序早已不是以前的那些由简单的增删改查拼凑而成的程序了,高复杂性早已是标配,而任务的定时调度与执行也是对程序的基本要求了。通过时间表达式来进行调度和执行的一类任务被称为定时任务,很多业务需求的实现都离不开定时任务。

在javascript中要实现定时任务也是很简单的,可以选择插件,也可以自己写一个简单的定时任务,这里就个给大家写一个简单的 setInterval() 循环定时任务。功能有,启动定时任务、停止定任务、添加定时任务、清除定时任务、执行定时器的方法。

实现调用演示

1.定义两个方法 有参和无参

function a1(value = "") {

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": item.year || "*",

"month": item.month || "*",

"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 (item.year != "*" && item.year != fullYear) {

continue

}

if (item.month != "*" && item.month != 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)

}

到此,关于“用JS写任务定时器的过程和代码是什么”的学习就结束了,希望能够解决大家的疑惑,另外大家动手实践也很重要,对大家加深理解和学习很有帮助。

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章