Check out my code in action here!
jQuery(document).ready(function(){
var insertionTemplate = jQuery(".hiddenAdBox").eq(0).html(),
insertionTarget = jQuery('ul'),
insertionTargetChildren = insertionTarget.find('li'),
insertionFrequency = 2;
var random;
for (var i = 0; i < insertionFrequency; i++) {
random = Math.floor(Math.random() * insertionTargetChildren.length) + 0;
insertionTargetChildren.eq(random).after(insertionTemplate);
}
});
I have a list of items and want to display ads randomly when the page is refreshed. However, with an ad count of 2 (insertionFrequency = 2
), there are times when they appear close together. How can I prevent this and ensure that ads do not show up near each other?
Here are screenshots for reference: https://i.sstatic.net/O3d9J.jpg