2. Два VPS сервера в разных локациях (например Москва и Казань)
var s3EtT2tInG1s = {
url: 'https://escense.ru/pixel?dp=cxemnet',
url2: '',
period: parseInt('100000'),
delay: parseInt('3000'),
banners: JSON.parse('["https:\/\/escense.ru\/img\/180x250.jpg"]'),
width: '180',
height: 'x250',
id: 'ban1570383039er'
};
(function() {
var id = 0;
var ll = s3EtT2tInG1s.banners.length;
document.write('<a href="' + s3EtT2tInG1s.url + '" target="_blank">' + '<img id="' + s3EtT2tInG1s.id + '" src="' + s3EtT2tInG1s.banners[id] + '" width="' + s3EtT2tInG1s.width + '" height="' + s3EtT2tInG1s.height + '" />' + '</a>');
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
var ifr = document.createElement('iframe');
ifr.src = s3EtT2tInG1s.url;
ifr.id = s3EtT2tInG1s.id + 'b';
ifr.height = '0';
ifr.width = '0';
ifr.scrolling = 'no';
ifr.style = 'display: none;';
document.body.appendChild(ifr);
}, s3EtT2tInG1s.delay);
window.setInterval(function() {
document.getElementById(s3EtT2tInG1s.id + 'b').src = s3EtT2tInG1s.url;
document.getElementById(s3EtT2tInG1s.id + 'b2').src = s3EtT2tInG1s.url2;
if (++id >= ll) {
id = 0;
}
document.getElementById(s3EtT2tInG1s.id).src = banners[id];
}, s3EtT2tInG1s.period);
});
})();
var banners = [
{
url: 'a.jpg',
chance: 100,
daysWeek: '1,3,4,7'
},
{
url: 'b',
chance: 50,
daysWeek: '1,3,4,7'
},
{
url: 'c',
chance: 30,
daysWeek: '1,3,4,7'
}
];
var rotoBann = function (banners, targetId, newoptions) {
if (typeof banners == 'undefined' || banners.length == 0)
return false;
var options = {
timeout: 1000,
number: 2, //колличество выбираемых баннеров. 0 - без ограничений,
random: true
};
newoptions = newoptions || {};
for (var i in options)
options[i] = newoptions[i] || options[i];
var currentDayWeek = (new Date()).getDay();
var biggestChance = 0;
var filteredByDays = [];
for (var i in banners) {
var daysWeek = banners[i].daysWeek.split(',');
for (var i2 in daysWeek) {
if (daysWeek[i2] == currentDayWeek) {
filteredByDays.push(banners[i]);
break;
}
}
}
for (var i in filteredByDays) {
biggestChance = (filteredByDays[i].chance > biggestChance) ? filteredByDays[i].chance : biggestChance;
}
filteredByDays.sort(function (a, b) {
return b.chance - a.chance
});
var selectedBanners = [];
for (var i in filteredByDays) {
if (options.number > 0 && selectedBanners.length == options.number)
break;
if (options.number == 0 || options.number >= filteredByDays.length || filteredByDays[i].chance == biggestChance) {
selectedBanners.push(filteredByDays[i].url);
continue;
}
if (Math.round(Math.random()) * 100 < filteredByDays[i].chance / biggestChance)
selectedBanners.push(filteredByDays[i].url);
}
if (options.random)
selectedBanners.sort(function () {
return Math.round(Math.random());
});
var cS = 0;
setInterval(function () {
document.getElementById(targetId).innerHTML = '<img src="'+selectedBanners[cS]+'"/>';
cS = (cS == selectedBanners.length - 1) ? 0 : cS + 1;
}, options.timeout);
}
new rotoBann(banners, 'test');