[debounce-version1](https://link.segmentfault.com/?enc=5M4djQn6RXb97dn5%2FIuSsg%3D%3D.M4IlNX7pUgrfVff4Fyn6tiHAcLHsW1lunvGhImsFK0hElPT8VqDDHU8lYs8DbmtFG8coFyIt9FDu%2BPtPWyqctw%3D%3D) [debounce-version2](https://link.segmentfault.com/?enc=H7bAur3zI9mr1Gy3yNsiaA%3D%3D.56UgzDeQ8o19EOVC95PM8kK68vn3zxLJZinmVNEXBWtkm2TQkd1DSp04CJ5ZeBZ52w0mTGJF44Y%2B%2FkLmrmlsog%3D%3D) version1 跟 version2的区别是移动了` if (notCalled && immediate) result = func.apply(context, args);` 的位置 我的预期是这两个代码应该执行结果是一致的,都返回1 但是version1 没有防抖成功 function debounce(func, wait, immediate) { var timeout var debounced = function () { var context = this; var args = arguments; var notCalled = !timeout; if (timeout) clearTimeout(timeout); if (notCalled && immediate) result = func.apply(context, args); timeout = setTimeout(function () { if (!immediate) func.apply(context, args); timeout = null; }, wait); }; return debounced; } var counter = 0; var debouncedIncr = debounce( function () { counter++; if (counter < 10) debouncedIncr(); }, 32, true, ); debouncedIncr(); console.log(counter, 1, "incr was called immediately"); setTimeout(function () { console.log(counter, 1, "incr was debounced"); }, 96);