Skip to content

Instantly share code, notes, and snippets.

@JustinDFuller
Created January 6, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinDFuller/ab65954961d76f15d8a273c99577d20b to your computer and use it in GitHub Desktop.
Save JustinDFuller/ab65954961d76f15d8a273c99577d20b to your computer and use it in GitHub Desktop.
Change video src based on loading times.
let loadingTimes = 0;
const maxLoadingTimesBeforeChange = 4;
const videoOptions = {
hd: 'myAwesomeVideoHD.mp4',
sd: 'myAwesomeVideoSD.mp4',
};
const video = `<video id='myVideo' src=${videoOptions.hd}></video>`;
const videoContainer = document.getElementById('myVideoContainerID');
videoContainer.innerHTML = video;
const videoElement = document.getElementById('myVideo');
videoElement.onstalled = loading;
videoElement.onwaiting = loading;
function loading () {
loadingTimes++;
if (loadingTimes > maxLoadingTimesBeforeChange) {
videoElement.setAttribute('src', videoOptions.sd);
videoElement.load();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment