Skip to content

Commit 03c88bd

Browse files
committed
Add small delay for auto-loading the video
1 parent accd012 commit 03c88bd

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

readme.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,24 @@ og_type: website
128128

129129
<script>
130130
var modal = document.getElementById('youtubeModal');
131-
modal.addEventListener('show.bs.modal', function () {
132-
var iframe = document.getElementById('youtubeVideo');
133-
var base = iframe.getAttribute('data-base');
134-
iframe.src = base + '?autoplay=1&rel=0';
131+
var playTimer; // delay handle
132+
133+
// Start auto-load/autoplay 0.5s after modal is fully shown
134+
modal.addEventListener('shown.bs.modal', function () {
135+
clearTimeout(playTimer);
136+
playTimer = setTimeout(function () {
137+
var iframe = document.getElementById('youtubeVideo');
138+
var base = iframe.getAttribute('data-base');
139+
iframe.src = base + '?autoplay=1&rel=0';
140+
}, 150);
135141
});
142+
143+
// If user closes before delay fires, cancel it
144+
modal.addEventListener('hide.bs.modal', function () {
145+
clearTimeout(playTimer);
146+
});
147+
148+
// Stop playback by resetting src after modal fully hidden
136149
modal.addEventListener('hidden.bs.modal', function () {
137150
var iframe = document.getElementById('youtubeVideo');
138151
iframe.src = iframe.getAttribute('data-base');

0 commit comments

Comments
 (0)