JavaScript如何实现手写原生任务定时器
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要介绍JavaScript如何实现手写原生任务定时器,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!功能介绍定时器顾名思义就是在某个特定的时间去执行一些任务,现代的应用
千家信息网最后更新 2025年11月08日JavaScript如何实现手写原生任务定时器
这篇文章主要介绍JavaScript如何实现手写原生任务定时器,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
功能介绍
定时器顾名思义就是在某个特定的时间去执行一些任务,现代的应用程序早已不是以前的那些由简单的增删改查拼凑而成的程序了,高复杂性早已是标配,而任务的定时调度与执行也是对程序的基本要求了。通过时间表达式来进行调度和执行的一类任务被称为定时任务,很多业务需求的实现都离不开定时任务。
在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)}以上是"JavaScript如何实现手写原生任务定时器"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
任务
定时器
方法
参数
时间
程序
内容
功能
篇文章
复杂
顾名思义
业务
两个
代码
价值
兴趣
复杂性
小伙
小伙伴
小时
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
黄浦区营销软件开发项目
工程软件开发平台
服务器与设备之间怎么连接
3d软件开发工具
存储过程 远程数据库
网络安全法执法检查报告
网络安全执行计划表
VC执行数据库语言失败
海康ntp服务器地址是多少
搭建tftp服务器
网络安全信息测试
计算机网络安全的特征体现
闪银 杭州网络技术
数据库设计思了解实验报告
闪电网络技术股份有限公司
广电总局 网络安全
广东咨询网络安全工程
疫情防控网络安全作文
火绒安全软件能装服务器吗
谈软件开发项目要问的问题
大连京科网络技术有限公司
服务器运行检查情况检查表
属于无线网络技术
无线传感器网络技术 实验
佛山软件开发工时
应用软件开发实践课题
数独游戏的软件开发概述
微软 软件开发平台
微软软件开发模式
安徽远闻网络技术有限公司