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>JS Bin</title>
</head>
<body>
<input id="demo"/>
 <script>
   function debounce(fn, delay) {
    var timer = null;
    return function() {
        clearTimeout(timer);
        var that = this,
            args = arguments;
        timer = setTimeout(function() {
            fn.apply(that, args)
        }, delay)
    }
}
function throttle(fn, delay) {
    var timer = null,
        flag = false;
    return function() {
        if (flag) {
            return
        } 
        flag = true
        var that = this,
            args = arguments;
        timer = setTimeout(function() {
            clearTimeout(timer);
            flag = false
            fn.apply(that, args)
        }, delay)
    }
}
// 节流函数
document.addEventListener('mousemove', throttle(()=> {console.log('3秒只能执行一次')},3000))
// 防抖函数
document.getElementById('demo').addEventListener('input', debounce(()=> {console.log('连续输入')},600))
  </script>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers