Skip to content

Add test cases #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add test cases #1

wants to merge 1 commit into from

Conversation

XiaoWinter
Copy link

I found a throttling method in the comments section that passed all the current tests but was wrong.So I added a test case to find this type of error.With this wrapping method, if the interval between two calls is greater than 1000ms, the wrapped method becomes invalid.

function throttle(func, period) {
  let savedArgs
  let savedThis
  let start = true

  function runFunc() {
    if (savedArgs) {
      func.apply(savedThis, savedArgs)
      savedArgs = undefined
      setTimeout(runFunc, period)
    }
  }

  function wrapper(...rest) {
    savedThis = this
    savedArgs = rest
    if (start) {
      start = false
      runFunc()
    }
  }
  return wrapper
}

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

Successfully merging this pull request may close these issues.

2 participants