I've coded a tab function that switches between tabs after a set period of time, this seemingly worked perfectly however, it's been bought to my attention that sometimes it breaks.
After looking into setInterval
and looking into the error itself it looks like setInterval
after 20-30 minutes, trips up on itself and starts a 2nd timer. This causes 2 tabs to show at once.
I've read a bit online where essentially it's to do with Javascript being single threaded and it's just a fundamental flaw with setInterval
that if it trips up it doesn't reset the timer it just creates a 2nd one. Is there anyway to add an error catch for this trip or a better way to run a repeating timeout function?
jQuery(document).ready(function() {
function autoPlayTabs(tabTitle1, tabTitle2, tabTitle3, tabTitle4, tabContent1, tabContent2, tabContent3, tabContent4, tabLength, tabTitleMobile1, tabTitleMobile2, tabTitleMobile3, tabTitleMobile4) {
var actualTabLength = tabLength * 4;
var tabContainer = jQuery('.elementor-tabs');
var allTabs = jQuery('.elementor-tabs .elementor-tabs-wrapper .elementor-tab-title');
var allContent = jQuery('.elementor-tabs .elementor-tabs-content-wrapper .elementor-tab-content');
var initialTabTimer = null;
var tabTimer = null;
tabTitle1.addClass('active'); tabContent1.addClass('active');
console.log('Setting initialTabTimer');
initialTabTimer = setTimeout(function(){
tabTitle1.removeClass('active'); tabContent1.removeClass('active');
tabTitle2.addClass('active'); tabContent2.addClass('active');
console.log("Setting Timeout2");
setTimeout(function(){
tabTitle2.removeClass('active'); tabContent2.removeClass('active');
tabTitle3.addClass('active'); tabContent3.addClass('active');
console.log("Setting Timeout3");
setTimeout(function(){
tabTitle3.removeClass('active'); tabContent3.removeClass('active');
tabTitle4.addClass('active'); tabContent4.addClass('active');
console.log("Executing Timeout3 Function");
}, tabLength);
}, tabLength);
}, tabLength);
console.log("Setting tabTimer");
tabTimer = setInterval(function(){
tabTitle4.removeClass('active'); tabContent4.removeClass('active');
tabTitle1.addClass('active'); tabContent1.addClass('active');
console.log("Setting TimeoutB");
setTimeout(function(){
tabTitle1.removeClass('active'); tabContent1.removeClass('active');
tabTitle2.addClass('active'); tabContent2.addClass('active');
console.log("Setting TimeoutC");
setTimeout(function(){
tabTitle2.removeClass('active'); tabContent2.removeClass('active');
tabTitle3.addClass('active'); tabContent3.addClass('active');
console.log("Setting TimeoutD");
setTimeout(function(){
tabTitle3.removeClass('active'); tabContent3.removeClass('active');
tabTitle4.addClass('active'); tabContent4.addClass('active');
console.log("Executing TimeoutD Function");
}, tabLength);
}, tabLength);
}, tabLength);
}, actualTabLength);
allTabs.click(function() {
if (initialTabTimer !== null) {
clearTimeout(initialTabTimer);
initialTabTimer = null;
console.log("Cleared initialTabTimer");
}
if (tabTimer !== null) {
clearInterval(tabTimer);
tabTimer = null;
console.log("Cleared tabTimer");
} else {
console.log("Did not need to clear tabTimer");
}
allTabs.removeClass('active');
allContent.removeClass('active');
tabContainer.addClass('tabsManual');
});
}
if(homeTabTitle1.length > 0){
console.log("Calling AutoPlayTabs Homepage");
autoPlayTabs(homeTabTitle1,homeTabTitle2,homeTabTitle3,homeTabTitle4,homeTabContent1,homeTabContent2,homeTabContent3,homeTabContent4,homeTabLength);
}
});
Below is a console.log of every setTimeout called from it working, to breaking to fixing itself:
Calling AutoPlayTabs Homepage
Setting initialTabTimer
Setting tabTimer
Setting Timeout2
Setting Timeout3
Executing Timeout3 Function
//This console.log block ran 30 times without error//
Setting TimeoutB
Setting TimeoutC
Setting TimeoutD
Executing TimeoutD Function
//This console.log block ran 30 times without error//
//It Breaks here after a total of 24 minutes//
Setting TimeoutB
Setting TimeoutC
Setting TimeoutD
Setting TimeoutB //For some reason TimeoutB has been fired again
Executing TimeoutD Function
Setting TimeoutC
Setting TimeoutD
Setting TimeoutB
Executing TimeoutD Function
Setting TimeoutC
Setting TimeoutD
Executing TimeoutD Function
//It fixed itself here after 2.4 minutes//
Setting TimeoutB
Setting TimeoutC
Setting TimeoutD
Executing TimeoutD Function