Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta name="description" content="debounce 饥人谷" />
  <meta charset="utf-8">
  <title>函数节流</title>
</head>
<body>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
  <h1>函数节流</h1>
</body>
</html>
 
function throttle(fn, gapTime) {
  let lastTime = null
  let nowTime = null
  return function() {
    nowTime = Date.now()
    if (!lastTime || nowTime - lastTime > gapTime) {
      fn()
      lastTime = nowTime
    }
  }
}
function fn() {
  console.log("节流")
}
fn = throttle(fn, 3000)
document.body.onscroll = function() {
  fn()
}
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers