function throttle(func, wait = 300) { let previous = 0 return function() { let now = date.now() if (now - previous > wait) { func.apply(this, arguments) previous = now } } } window.onload = () => { const header = document.queryselector('.header') const { offsetheight } = header window.addeventlistener('scroll', throttle(function () { const initialtopvalue = 50 const scrolly = window.scrolly console.log('scrolly', scrolly) if (scrolly > offsetheight + initialtopvalue) { header.classlist.add('sticky') } else { header.classlist.remove('sticky') } })) }