I made this javascript method that I altered from an existing script that I found online that should rotate showing an indefinite number of '.testimonial' divs. The script works fine in chrome and firefox, but doesn’t compile in internet explorer, unless you use f12 to start the debugging of the script. Is there a better way to write this script? I have looked online for ideas but haven’t been able to find anything. I imagine that the issue is with the console.log(testimonialCount); statement, but am unsure of a better way to write it. Any help would be greatly appreciated. Thanks.
//rotate testimonials script
jQuery('.testimonial').hide();
var testimonialCount = $('.testimonial').length;
console.log(testimonialCount );
var currentItem = 0;
var timeout;
timeout = window.setTimeout((function(){switchDiv();}));
switchDiv = function() {
if (currentItem == testimonialCount - 1) {
jQuery('.testimonial').eq(testimonialCount - 1).hide();
currentItem = 0;
jQuery('.testimonial').eq(0).fadeIn();
timeout = window.setTimeout((function(){switchDiv();}),7000);
}
else {
jQuery('.testimonial').eq(currentItem).hide();
currentItem = currentItem + 1;
jQuery('.testimonial').eq(currentItem).fadeIn();
timeout = window.setTimeout((function(){switchDiv();}),7000);
}
}
When the IE developer tools are not open, there is no
consoleobject.Therefore, calling
console.log()throws an error.To prevent that, you can check
if ('console' in window)and make your own dummy console (or just don’t log anything) if it isn’t.