// 节流
const video = document.querySelector('.video')
// 视频加载完成时,读取并设置播放时间
video.addEventListener('loadeddata',function(){
video.currentTime = localStorage.getItem('currentTime') || 0
})
// 使用节流优化
let timeId
video.addEventListener('timeupdate',function(){
if(timeId !== undefined){
return
}
timeId = setTimeout(()=>{
console.log('timeupdate!')
localStorage.setItem('currentTime',this.currentTime)
// 重置timeId
timeId = undefined
},500)
})
JS详解-手写节流
![](https://img-home.csdnimg.cn/images/20240711042549.png)