JavaScript實現(xiàn)手寫原生任務定時器
功能介紹
定時器顧名思義就是在某個特定的時間去執(zhí)行一些任務,現(xiàn)代的應用程序早已不是以前的那些由簡單的增刪改查拼湊而成的程序了,高復雜性早已是標配,而任務的定時調度與執(zhí)行也是對程序的基本要求了。通過時間表達式來進行調度和執(zhí)行的一類任務被稱為定時任務,很多業(yè)務需求的實現(xiàn)都離不開定時任務。
在javascript中要實現(xiàn)定時任務也是很簡單的,可以選擇插件,也可以自己寫一個簡單的定時任務,這里就個給大家寫一個簡單的 setInterval() 循環(huán)定時任務。功能有,啟動定時任務、停止定任務、添加定時任務、清除定時任務、執(zhí)行定時器的方法。
實現(xiàn)調用演示
1.定義兩個方法 有參和無參
function a1(value = "") {
console.log("定時任務被調用了 攜帶的參數(shù)==>>", value)
}
function a2() {
console.log("定時任務被調用了 我是無參數(shù)")
}2. 實現(xiàn)定時任務
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()創(chuàng)建定時任務js 代碼文件 復制使用即可
bg-timer.js
// 緩存定時任務
// {
// func 方法名
// parm 方法參數(shù)
// year 年
// month 月
// date 日
// hours 時
// minutes 分
// seconds 秒
// }
// * 表示每刻都執(zhí)行 數(shù)字 表示定時這個時間執(zhí)行
/**
* 每年 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實現(xiàn)手寫原生任務定時器的文章就介紹到這了,更多相關JavaScript任務定時器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript模擬git diff命令實現(xiàn)文本文件差異比較
這篇文章主要為大家詳細介紹了javascript如何模擬git diff命令實現(xiàn)文本文件差異比較效果,文中的示例代碼講解詳細,感興趣的小伙伴可以參考下2023-12-12
JS實現(xiàn)動態(tài)生成表格并提交表格數(shù)據(jù)向后端
這篇文章主要為大家詳細介紹了JS實現(xiàn)動態(tài)生成表格并提交表格數(shù)據(jù)向后端的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
JavaScript回調函數(shù)callback用法解析
這篇文章主要介紹了JavaScript回調函數(shù)callback用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
使用Require.js封裝原生js輪播圖的實現(xiàn)代碼
這篇文章主要介紹了使用Require.js封裝原生js輪播圖的實現(xiàn)代碼,需要的朋友可以參考下2017-06-06
Javascript 函數(shù)parseInt()轉換時出現(xiàn)bug問題
天測試的測出來的。parseInt(1.13*100),實際返回值是112,下面有個示例,大家可以看看下2014-05-05
layer頁面跳轉,獲取html子節(jié)點元素的值方法
今天小編就為大家分享一篇layer頁面跳轉,獲取html子節(jié)點元素的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09

