Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save video currenttime to local storage and get the time back when the page is reloaded also set video currenttime when you scrub the video. #521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 11 - Custom Video Player/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
2 changes: 1 addition & 1 deletion 11 - Custom Video Player/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
</div>
</div>

<script src="scripts.js"></script>
<script src="scripts-FINISHED.js"></script>
</body>
</html>
15 changes: 14 additions & 1 deletion 11 - Custom Video Player/scripts-FINISHED.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ const toggle = player.querySelector('.toggle');
const skipButtons = player.querySelectorAll('[data-skip]');
const ranges = player.querySelectorAll('.player__slider');

let currentVideoTime = localStorage.getItem('currentVideoTime') || 0;

/* Build out functions */
function togglePlay() {
const method = video.paused ? 'play' : 'pause';
video[method]();
currentVideoTime= video.currentTime;
localStorage.setItem('currentVideoTime', currentVideoTime);
}

window.addEventListener('DOMContentLoaded', (event) => {
localStorage.setItem('currentVideoTime', currentVideoTime);
currentVideoTime = parseFloat(localStorage.getItem('currentVideoTime')) || 0;
video.currentTime = currentVideoTime;
});

function updateButton() {
const icon = this.paused ? '►' : '❚ ❚';
console.log(icon);
Expand All @@ -34,7 +44,10 @@ function handleProgress() {

function scrub(e) {
const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
video.currentTime = scrubTime;
// video.currentTime = scrubTime;
localStorage.setItem('currentVideoTime', scrubTime);
currentVideoTime = parseFloat(localStorage.getItem('currentVideoTime')) || 0;
video.currentTime = currentVideoTime;
}

/* Hook up the event listeners */
Expand Down