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

[js] 第630天 写一个方法实现指定开始的数字增加到指定的结束数字,步长默认为1 #3395

Open
haizhilin2013 opened this issue Jan 4, 2021 · 1 comment
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第630天 写一个方法实现指定开始的数字增加到指定的结束数字,步长默认为1

3+1官网

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Jan 4, 2021
@dawnswwwww
Copy link

/**
*

  • @param Number start
  • @param Number end
  • @param Number step default = 1
  • @param Number delay (ms) delay to exec
  • @param Function cb callback, have two arguments: currentNumber, end
    */
    function addTo(start, end, step = 1, delay, cb) {
    if (start >= end ) {
    start = end
    cb && cb(start, end)
    return
    }
    return new Promise((resolve, reject) => {
    if (delay) {
    setTimeout(() => {
    cb && cb(start, end)
    start = start += step
    addTo(start, end, step, delay, cb)
    }, delay);
    } else {
    cb && cb(start, end)
    start = start += step
    addTo(start, end, step, delay, cb)
    }
    })
    }
    // test
    addTo(2, 100, 4, null, console.log)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

2 participants