sggmico / fe-happy-interview

面试不迷茫
Apache License 2.0
5 stars 0 forks source link

编写函数mySetInterval(fn, a, b)。每次间隔 a,a+b,a+2b,...,a+nb 的时间执行fn?如何停止 mySetInterval? #1

Open sggmico opened 4 years ago

sggmico commented 4 years ago

大佬!👇 👇 👇

sggmico commented 4 years ago
// 启动计时
function mySetInterval(fn, a, b) {
    let n = 0,
        tag = {
            timer: 0
        }
    function innter() {
        tag.timer = window.setTimeout(() => {
            fn()
            ++n
            innter()
        }, a + n * b)

    }
    innter()
    return tag
}
// 清除计时
function myClear(tag) {
    window.clearTimeout(tag.timer)
}