4

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?

anana
  • 127
  • 1
  • 6
  • Can the script be loaded after the page load? Some libraries can not because of document.write or binding to window onload. – epascarello Mar 25 '19 at 18:31
  • 1
    Side note; naming variables with a single character greatly reduces the readability and maintainability of your code. You should consider names that self document your code, and use an uglifier and minimizer to reduce the size of your code only when it goes into production. – Taplar Mar 25 '19 at 18:38
  • Did you try to put a `setTimeout` inside the `var l = function () {` – Core972 Jun 06 '19 at 14:49

1 Answers1

4

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

  • okay so this mainly adds a `setTimeout(..., 5000)`, e.g. 5 seconds delay before loading intercom – Dorian Dec 05 '21 at 08:56