Skip to content

Commit

Permalink
option to allow turning off reloading observer
Browse files Browse the repository at this point in the history
  • Loading branch information
slawomir-jedlikowski committed Oct 30, 2018
1 parent 08e9994 commit 5a661b6
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/ad-refresher.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
options = options || {};
options.loadingThreshold = options.loadingThreshold || LOADING_AD_POSITION_THRESHOLD;
options.reloadingThreshold = options.reloadingThreshold || RELOADING_AD_POSITION_THRESHOLD;
options.useReloading = options.useReloading || true;

var loader = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
Expand All @@ -27,21 +28,24 @@
}, {
rootMargin: '0px 0px ' + options.loadingThreshold + 'px 0px'
});
var reloader = new IntersectionObserver(function(entries) {
entries.forEach(function(entry) {
var currentY = entry.boundingClientRect.y;
var currentRatio = entry.intersectionRatio;
if (entry.isIntersecting && currentRatio >= lastRatio && currentY > lastY) {
entry.target.dispatchEvent(new Event('reload-ad'));
}
lastY = currentY;
lastRatio = currentRatio;
});
}, {
rootMargin: options.reloadingThreshold + 'px 0px 00px 0px'
});
loader.observe(element);
reloader.observe(element);

if (options.useReloading) {
var reloader = new IntersectionObserver(function(entries) {
entries.forEach(function(entry) {
var currentY = entry.boundingClientRect.y;
var currentRatio = entry.intersectionRatio;
if (entry.isIntersecting && currentRatio >= lastRatio && currentY > lastY) {
entry.target.dispatchEvent(new Event('reload-ad'));
}
lastY = currentY;
lastRatio = currentRatio;
});
}, {
rootMargin: options.reloadingThreshold + 'px 0px 0px 0px'
});
reloader.observe(element);
}
}

var AdRefresher = {
Expand Down

0 comments on commit 5a661b6

Please sign in to comment.