Skip to content

Instantly share code, notes, and snippets.

@alkrauss48
Created December 22, 2025 20:34
Show Gist options
  • Select an option

  • Save alkrauss48/9fa7a235a24ff115acdefe5a7d6f6f7a to your computer and use it in GitHub Desktop.

Select an option

Save alkrauss48/9fa7a235a24ff115acdefe5a7d6f6f7a to your computer and use it in GitHub Desktop.
// Find a <video> tag by class, and set the currentTime equal to its duration less one second.
function jumpToSecondBeforeEnd() {
var className = "myClass";
var video = document.getElementsByClassName(className)[0];
// Check if the video metadata is loaded and duration is available
if (!isNaN(video.duration) && isFinite(video.duration)) {
// Calculate the target time: duration minus 1 second
var targetTime = video.duration - 1;
// Ensure the target time is not before the beginning of the video
if (targetTime >= 0) {
video.currentTime = targetTime;
// Optional: start playing immediately
// video.play();
} else {
console.log("Video is less than 1 second long.");
}
} else {
console.log("Video duration is not yet available. Try again after the video loads metadata.");
// You can add an event listener here to run the function when metadata loads
video.addEventListener('loadedmetadata', jumpToSecondBeforeEnd, { once: true });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment