简介
每次打开页面视频从上一次的播放位置开始播放
利用lodash库做节流
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
video {
width: 800px;
height: 400px;
}
</style>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<video src="https://v.itheima.net/LapADhV6.mp4" controls></video>
<script>
//视频节流
const video = document.querySelector('video');
//从本地存储拿取上次播放时长位置
(function () {
video.currentTime = localStorage.getItem('playTime')
})()
video.addEventListener('timeupdate', _.throttle(() =>{
localStorage.setItem('playTime', video.currentTime)
}, 1000))
</script>
</body>
</html>