Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed loading intercom script in website

I am trying to load the intercom script with a delay of 5 sec.

I try to use a function setTimeout but then the intercom does not start

setTimeout(function () {
    function inter() {
        var w = window;
        var ic = w.Intercom;
        if (typeof ic === "function") {
            ic('reattach_activator');
            ic('update', w.intercomSettings);
        } else {
            var d = document;
            var i = function () {
                i.c(arguments);
            };
            i.q = [];
            i.c = function (args) {
                i.q.push(args);
            };
            w.Intercom = i;
            var l = function () {
                var s = d.createElement('script');
                s.type = 'text/javascript';
                s.async = true;
                s.src = 'https://widget.intercom.io/widget/my_id';
                var x = d.getElementsByTagName('script')[0];
                x.parentNode.insertBefore(s, x);
            };
            if (w.attachEvent) {
                w.attachEvent('onload', l);
            } else {
                w.addEventListener('load', l, false);
            }
        }
        console.log("test")
    };

    inter();
}, 5000);

Test consol log appears after 5s, but not intercom. When i not use function setTimeout intercome start fine. Any ideas?

like image 369
anana Avatar asked Nov 04 '25 04:11

anana


1 Answers

try this

<script>
(function () {
    var w = window;
    var ic = w.Intercom;
    if (typeof ic === "function") {
        ic('reattach_activator');
        ic('update', w.intercomSettings);
    } else {
        var d = document;
        var i = function () {
            i.c(arguments);
        };
        i.q = [];
        i.c = function (args) {
            i.q.push(args);
        };
        w.Intercom = i;
        var l = function () {
            setTimeout(function () {
                var s = d.createElement('script');
                s.type = 'text/javascript';
                s.async = true;
                s.src = 'https://widget.intercom.io/widget/yourID';
                var x = d.getElementsByTagName('script')[0];
                x.parentNode.insertBefore(s, x);
            }, 5000);
        };
        if (w.attachEvent) {
            w.attachEvent('onload', l);
        } else {
            w.addEventListener('load', l, false);
        }
    }
})();

Not sure you need it anymore but might help someone in the future

like image 109
Kalipsso Marius Avatar answered Nov 06 '25 19:11

Kalipsso Marius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!