欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實現(xiàn)手寫原生任務(wù)定時器

 更新時間:2022年03月05日 11:01:17   作者:菜的慌  
定時器顧名思義就是在某個特定的時間去執(zhí)行一些任務(wù),現(xiàn)代的應(yīng)用程序早已不是以前的那些由簡單的增刪改查拼湊而成的程序了,高復(fù)雜性早已是標(biāo)配,而任務(wù)的定時調(diào)度與執(zhí)行也是對程序的基本要求了。本文將利用JavaScript手寫原生任務(wù)定時器,需要的可以參考一下

功能介紹

定時器顧名思義就是在某個特定的時間去執(zhí)行一些任務(wù),現(xiàn)代的應(yīng)用程序早已不是以前的那些由簡單的增刪改查拼湊而成的程序了,高復(fù)雜性早已是標(biāo)配,而任務(wù)的定時調(diào)度與執(zhí)行也是對程序的基本要求了。通過時間表達(dá)式來進(jìn)行調(diào)度和執(zhí)行的一類任務(wù)被稱為定時任務(wù),很多業(yè)務(wù)需求的實現(xiàn)都離不開定時任務(wù)。

在javascript中要實現(xiàn)定時任務(wù)也是很簡單的,可以選擇插件,也可以自己寫一個簡單的定時任務(wù),這里就個給大家寫一個簡單的 setInterval() 循環(huán)定時任務(wù)。功能有,啟動定時任務(wù)、停止定任務(wù)、添加定時任務(wù)、清除定時任務(wù)、執(zhí)行定時器的方法。

實現(xiàn)調(diào)用演示

1.定義兩個方法 有參和無參

function a1(value = "") {
            console.log("定時任務(wù)被調(diào)用了 攜帶的參數(shù)==>>", value)
        }

        function a2() {
            console.log("定時任務(wù)被調(diào)用了 我是無參數(shù)")
        }

2. 實現(xiàn)定時任務(wù)

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)建定時任務(wù)js 代碼文件 復(fù)制使用即可

bg-timer.js

// 緩存定時任務(wù)
// {
//     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("啟動定時任務(wù)")
    if (!is_timer) {
        timeoutFunc()
    }
}

// 停止
function stopTimer() {
    console.log("停止定時任務(wù)")
    if (is_timer) {
        clearInterval(is_timer);
        is_timer = null
    }
}

// 清除定時任務(wù)
function cleanTimer() {
    stopTimer()
    timer_list = []
}


// 添加定時任務(wù)
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("調(diào)用定時任務(wù)", item)
            if (item.func && item.parm) {
                item.func(item.parm)
            } else {
                item.func()
            }
        }
    }, 1000)
}

到此這篇關(guān)于JavaScript實現(xiàn)手寫原生任務(wù)定時器的文章就介紹到這了,更多相關(guān)JavaScript任務(wù)定時器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論