Skip to content

Duplicated afterEachAdLoaded when $.dfp() has been called more than once #135

@tatai

Description

@tatai

Hi,

First of all, thanks for creating this plugin.

I have a page with an infinite scroll and I have followed the steps given in the instructions and also explained in some issues like #112, but when I create a callback to afterEachAdLoaded, each time I invoke dfp() to define the new ad, afterEachAdLoaded receives a repeated callback.

I have followed the code and I think the issue is related to the pubadsService.addEventListener('slotRenderEnded') made on line 137. This listener is added with each call to dfp().

I cannot show public code right now, but it can be reproduced with:

var options = {
    dfpID: 'xxxxxxxxx',
    enableSingleRequest: false,
    afterEachAdLoaded: function(ad) {
        console.log('afterEachAdLoaded', ad);
    }
};

$(document).ready(function() {
    $.dfp(options);
});

// This event is triggered when a new page is loaded using ajax
$(document).on('new-page-loaded', function($page) {
    console.log('New page');
    $page.find('.adunit').dfp(options);
});

The output shown in the console will be something like:

afterEachAdLoaded banner1
New page
afterEachAdLoaded banner2
afterEachAdLoaded banner2

After some develop I have created a version that distinguishes between "init" and "new ad" calls.

I am going to make a pull request for your consideration. I have made a strong effort to make it backward-compatible with current code.

I have not added the minified version or propose changes in the readme prior to your comments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions