Skip to content

Commit 221c8be

Browse files
committed
add timer to switch slides automatically
1 parent 761345f commit 221c8be

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

resources/js/functions.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,40 @@ $(document).ready(function () {
323323
}
324324
});
325325

326+
$(document).ready(function () {
327+
// set up automatic switching of code carousel
328+
$(".code-carousel").each(function () {
329+
var carousel = this;
330+
var inputs = [];
331+
$(carousel).children("input.code-carousel_control").each(function () {
332+
inputs.push(this);
333+
});
334+
335+
// if there is more than one section, set up automatic switching
336+
if (inputs.length > 1) {
337+
338+
var cancelled = false;
339+
340+
var index = inputs.findIndex(input => input.checked);
341+
342+
// switch every 8 seconds while the page is visible and not cancelled
343+
const intervalHandle = setInterval(() => {
344+
if (!document.hidden && !cancelled) {
345+
const nextIndex = (index + 1) % inputs.length;
346+
inputs[nextIndex].checked = true;
347+
index = nextIndex;
348+
}
349+
}, 8000);
350+
351+
carousel.addEventListener("click", function cancelTicker() {
352+
carousel.removeEventListener("click", cancelTicker);
353+
cancelled = true;
354+
clearInterval(intervalHandle);
355+
});
356+
}
357+
});
358+
});
359+
326360
var image = { width: 1680, height: 1100 };
327361
var target = { x: 1028, y: 290 };
328362

0 commit comments

Comments
 (0)