1. // throttle的简易实现
    2. // function throttle(func, duration) {
    3. // 在这里编写具体实现
    4. // }
    5. // window.addEventListener('scroll', throttle(func, 50), false);
    6. function throttle (func, duration) {
    7. this.latest = 0
    8. return function () {
    9. let cur = +new Date()
    10. if (duration < cur - this.latest) {
    11. func.apply(this, arguments)
    12. this.latest = cur
    13. }
    14. }
    15. }
    16. const func = function () {
    17. console.log('scroll')
    18. }
    19. window.addEventListener('scroll', throttle(func, 50), false)