溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JavaScript如何實現手寫原生任務定時器

發布時間:2022-03-05 14:06:06 來源:億速云 閱讀:205 作者:小新 欄目:開發技術

這篇文章主要介紹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如何實現手寫原生任務定時器”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女