只需添加事件偵聽器,在調用后將其刪除,然后設置超時以再次添加。 function scrollHandler() { window.removeEventListener('scroll', scrollHandler); // Detects new state and compares it with the old one if ((document.body.getBoundingClientRect()).top > iniState) console.log('up'); else console.log('down'); // Saves the new position for iteration. iniState = (document.body.getBoundingClientRect()).top; setTimeout(() => window.addEventListener('scroll', scrollHandler), 2000);}window.addEventListener('scroll', scrollHandler);