首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于某些原因不能调用函数-对脚本使用localStorage

由于某些原因不能调用函数-对脚本使用localStorage
EN

Stack Overflow用户
提问于 2020-02-28 15:11:27
回答 1查看 47关注 0票数 0

我试图做的是控制台记录脚本中未读消息的数量。规则是:如果聊天窗口被最小化,在收到消息时添加一个,当聊天窗口最大化时,将计数设置为零。所有存储为持久化,因此页面重新加载或导航不会更改计数。这是我的剧本。

代码语言:javascript
复制
      count = 0;
      let is_minimized = true;

      var chatServiceOptions = {
        license: "*SECRET*", // ie '1234567'
        group: 4, // ie 22,
        customer: {
          name: "Development Account",
          timezone: "Europe/London"
        },
        plugins: [

// this is where the code starts          
          function countAdd() {
                localStorage.count = count++;
              },
//this is just calling the API, when a chat is available          
          function (chatService) {
           chatService.register('ready', function() {

             // when chat window is closed, ensure is_minimized is true
             chatService.events.on('LC_on_chat_window_minimized', data => {
               is_minimized = true
             });

             // when chat window is opened, set is_minimized to false
         chatService.events.on('LC_on_chat_window_opened', data => {
                count = 0    
                is_minimized = false
             });

             chatService.events.on('LC_on_message', function(data) {
               // set the window minimized to a variable 
               var LC_API = LC_API || {};
                  LC_API.on_chat_window_minimized = function() {
                    is_minimized = true;
               };

               // output the data of the incoming/outgoing message
               console.log(data);
               // check that it comes from an agent and the chat window is minimized
               // if it is, add +1 to unread message count
               if (data.user_type ==='agent' && is_minimized == true) {
                countAdd(); } 
               // if its maximized, set unread count to zero
               else { localStorage.count = 0; };

               // display number of unread messages
               console.log('unread messages: ' + localStorage.getItem('count'));
             });
           })
          }
     ],
      };
      var LC_API = LC_API || {};
LC_API.on_message = function(data) {
  console.log(data)
  alert("Message " + data.text + " sent by " + data.user_type);
};
    </script>

控制台给了我:

Uncaught : countAdd未定义为

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-28 15:16:07

您需要首先定义countAdd和其他函数,然后将它们传递到插件数组中。由于您是内联地定义它们,所以第二个函数不知道countAdd

代码语言:javascript
复制
function countAdd() { }
function otherFn() {
  // uses countAdd in here
}

var chatServiceOptions = {
  license: "*SECRET*", // ie '1234567'
  group: 4, // ie 22,
  customer: {
    name: "Development Account",
    timezone: "Europe/London"
  },
  // provide both functions to the plugins property
  plugins: [countAdd, otherFn]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60454470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档