Customising/Modifying inline content loaded from third party widgets using JavaScript function which repeats after regular intervals for fixed number of times.

Recently, I was working on some third-party widget integration where the content of widget was loaded after the page load. We were having the requirement of customising the content that was loaded in the widget. Some of the requirements were like hiding some content, changing text of the buttons, etc.

In this article, we will see how we can achieve that with JavaScript repeat function.

JavaScript function which repeats after regular intervals for fixed number of times

var CustomizeWidget = {
    // repeatFunc: function which repeats after regular intervals for fixed number of times.
    repeatFunc: function (func, delay, iterations) {
        var x = 0;
        var id = window.setInterval(function () {
            func();
            x = x + 1;

            if (x === iterations) {
                window.clearInterval(id);
            }
        }, delay);

        return id;
    }
};

Customising/Modifying inline content loaded from third party widgets

var CustomizeWidget = {
    init: function () {
        CustomizeWidget.customize();
    },

    // repeatFunc: function which repeats after regular intervals for fixed number of times.
    repeatFunc: function (func, delay, iterations) {
        var x = 0;
        var id = window.setInterval(function () {
            func();
            x = x + 1;

            if (x === iterations) {
                window.clearInterval(id);
            }
        }, delay);

        return id;
    },

    // customize: function for customising/modifying inline content loaded from third party widgets.
    customize: function () {
        var interval = CustomizeWidget.repeatFunc(function () {
            // Changing button text & style.
            var widgetButtonID = $('#widgetButtonID');
            if (widgetButtonID.length > 0) {
                widgetButtonID.css({ "background-color": "yellow", "font-size": "200%" });
                widgetButtonID.html('Custom Button Text');

                // stop the repititions after, we are done with the changes.
                clearInterval(interval);
            }
        }, 100, 50);
    }
};

$(function () {
    CustomizeWidget.init();
});

That’s it. Happy coding !!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *