Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>自动保存文件案例</title>
</head>
<body>
  <textarea id="box" cols="50" rows="10"></textarea>
  <p>保存<span id="count">0</span></p>
</body>
</html>
 
const $ = s => document.querySelector(s)
let count = 0
save = throttle(save, 1000 * 3)
$('#box').oninput = function() {
  save()
}
function save() {
  $('#count').innerText = ++count
}
function throttle(fn, gapTime) {
  let lastTime = null
  let nowTime = null
  return function() {
    nowTime = Date.now()
    if (!lastTime || nowTime - lastTime > gapTime) {
      fn()
      lastTime = nowTime
    }
  }
}
Output

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