0

按照本文档的说明,我最终得到以下代码:

window.zEmbed || (function (d, s) {
          var z = $zopim = function (c) { z._.push(c) },
              $ = z.s =
          d.createElement(s), e = d.getElementsByTagName(s)[0]; z.set = function (o) {
              z.set.
              _.push(o)
          }; z._ = []; z.set._ = []; $.async = !0; $.setAttribute("charset", "utf-8");
          $.onload = (event) => {
              console.log("chat script has loaded");
          };
          $.src = "//v2.zopim.com/?@zopimClientID"; z.t = +new Date; $.
              type = "text/javascript"; e.parentNode.insertBefore($, e);   
      })(document, "script");

      var onChatStart = function () {
          $zopim.livechat.window.show();
          window['onChatStart'] && window['onChatStart']();
      };

      $zopim(function () {
          $zopim.livechat.clearAll();
          (......)
      });

问题是$zopim(funtion() {..})从未调用回调函数,但脚本已成功加载。我知道基于由以下人员制作的console.log:

$.onload = (event) => {
     console.log("chat script has loaded");
};

有谁知道为什么会这样?谢谢

4

1 回答 1

0

问题是您在$zopim完全加载之前运行脚本。一旦对象的所有部分$zopim都加载完毕,它将停止循环并运行您的脚本。

var zopimConfig = function() {
    $zopim(function() {
        $zopim.livechat.clearAll();
        (......)
    });
};

var waitForZopim = setInterval(function () {
    if (
        window.$zopim === undefined ||
        window.$zopim.livechat === undefined ||
        window.$zopim.livechat.departments === undefined
    ) {
        return;
    }
    zopimConfig();
    clearInterval(waitForZopim);
}, 100);

等待window.$zopim.livechat.departments是可选的,但是如果你在部门周围做任何事情,我建议保留这个

于 2019-05-22T11:40:39.137 回答