Skip to content

Commit 5527762

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

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

resources/js/functions.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,48 @@ $(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+
// at every timeout, switch to the next section if the page is visible
339+
const Ticker = (timeout) => {
340+
var cancelled = false;
341+
342+
const interval = setInterval(() => {
343+
if (!document.hidden && !cancelled) {
344+
var currentlyChecked = inputs.findIndex(input => input.checked);
345+
const index = (currentlyChecked + 1) % inputs.length;
346+
inputs[index].checked = true;
347+
}
348+
}, timeout);
349+
350+
const cancel = () => {
351+
cancelled = true;
352+
clearInterval(interval);
353+
};
354+
355+
return { cancel };
356+
};
357+
358+
const ticker = Ticker(8000); // switch every 8 seconds
359+
360+
carousel.addEventListener("click", function cancelTicker() {
361+
carousel.removeEventListener("click", cancelTicker);
362+
ticker.cancel();
363+
});
364+
}
365+
});
366+
});
367+
326368
var image = { width: 1680, height: 1100 };
327369
var target = { x: 1028, y: 290 };
328370

0 commit comments

Comments
 (0)