Skip to content

写一个 mySetInterVal(fn, a, b),每次间隔 a,a+b,a+2b 的时间,然后写一个 myClear,停止上面的 mySetInterVal #99

@AwesomeDevin

Description

@AwesomeDevin
function mySetInterval(fn, a, b){
  let timer = null
  let count = 0
  let timestamp
  
// 设置定时器
  function set(){
    const step = count % 3
    switch(step){
      case 0: 
        timestamp = a;
        break;
      case 1:
        timestamp = a + b
        break;
      case 2:
        timestamp = a + 2 * b
        break;
    }
    
    timer = setTimeout(()=>{
      fn()
      count++
      set()
    }, timestamp * 1000)
  }
  
// 清除定时器
  function clear(){
    timer && clearTimeout(timer)
  }
  
  set()
  
  return clear
}

function out(){
  console.log(new Date().getSeconds())
}

const clear = mySetInterval(out,1,2)
setTimeout(()=>{clear()},20000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions