Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AwesomeDevin opened this issue Sep 18, 2023 · 0 comments

Comments

@AwesomeDevin
Copy link
Owner

AwesomeDevin commented Sep 18, 2023

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant