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

练习题4:间隔特定的时间执行函数 #15

Open
chenhuiYj opened this issue Jun 17, 2020 · 0 comments
Open

练习题4:间隔特定的时间执行函数 #15

chenhuiYj opened this issue Jun 17, 2020 · 0 comments

Comments

@chenhuiYj
Copy link
Owner

有若干和函数(fn1,fn2,....fnn),需要编写一个函数,进行间隔特定的时间执行特定的函数

比如 隔间 1 秒执行 fn1 ,之后间隔 2 秒执行 fn2

比如 隔间立即执行 fn1,之后间隔 4 秒执行 fn3

function sleep (time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}
async function fn(arr){
    for(let item of arr){
        await sleep(item.time).then(()=>{item.fn()})
    }

}
fn([
    {
        time:1000,
        fn(){
            console.log('1秒后执行fn1')
        }
    },
    {
        time:2000,
        fn(){
            console.log('2秒后执行fn2')
        }
    }
])

fn([
    {
        time:0,
        fn(){
            console.log('立即执行fn1')
        }
    },
    {
        time:4000,
        fn(){
            console.log('4秒后执行fn2')
        }
    }
])
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