用JS写任务定时器的过程和代码是什么
Admin 2022-11-15 群英技术资讯 461 次浏览
功能介绍
定时器顾名思义就是在某个特定的时间去执行一些任务,现代的应用程序早已不是以前的那些由简单的增删改查拼凑而成的程序了,高复杂性早已是标配,而任务的定时调度与执行也是对程序的基本要求了。通过时间表达式来进行调度和执行的一类任务被称为定时任务,很多业务需求的实现都离不开定时任务。
在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写任务定时器的过程和代码是什么”的学习就结束了,希望能够解决大家的疑惑,另外大家动手实践也很重要,对大家加深理解和学习很有帮助。如果想要学习更多的相关知识,欢迎关注群英网络,小编每天都会给大家分享实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
前几天做到一个关于图片上传功能,下面这篇文章主要给大家介绍了关于vue使用element实现上传图片和修改图片功能的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
这篇文章主要介绍了Javascript闭包的使用场景, 由于在Javascript语言中,只有函数内部的子函数才能读取局部变量,闭包就是能够读取其他函数内部变量的函数。所以,在本质上,闭包就是将函数内部和函数外部连接起来的一座桥梁,下面一起进入文化在哪个了解文章内容
本篇文章给大家带来了关于javascript的相关知识,其中主要介绍了关于基础语法的相关问题,JavaScript是互联网上最流行的脚本语言,下面就一起来看一下,希望对大家有帮助。
如何为 Node.js 的 require 函数添加钩子?下面本篇文章就来带大家了解一下require函数中添加钩子的方法,希望对大家有所帮助!
这篇文章给大家分享的是有关使用JS制作音乐导航的效果内容,实现效果如下,小编觉得比较有缺,对大家学习JavaScript有一定的帮助,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008