jquery.js:未捕获的ReferenceError:未定义connect

时间:2018-02-23 11:26:29

标签: javascript jquery

我在带有数据库的网站上运行此代码,这与ambiance.js,jquery.js(3.2.1)和socket.js交互,但我相信这需要我省略的其他依赖。

google chrome console

var SOCKET = null;
var user = null;

 $(document).ready(function() {

    connect();
});

function request(msg)
{ 
var m=msg;
if(m.type == 'aMessage')
   {
       console.log(m.msg);   
   }
  function connect()
    { 
       if(!SOCKET)
       { 
           var hash = getCookie('hash');
           if (hash == '')
           {
               $.ambiance({message: 'Please login!'});
           }
           else
           {
               $.ambiance({message: 'Connecting to server..', type: 'success'});                  
           }
           SOCKET = io(':4095');
           SOCKET.on('connect', function(msg)
          {
               if(hash != '')
               {
                   $.ambiance({message: 'Connected', type: 'success'});
               }
               SOCKET.emit('hash', {
                   hash: hash
               });
          });
           SOCKET.on('connect_error', function(msg){
               $.ambiance({message: 'Connection lost', type: 'error'});
           });

           SOCKET.on('request', function(msg){
               request(msg);
           });
        }
    }
}

1 个答案:

答案 0 :(得分:3)

实际上,您在请求函数中创建了connect()函数(尚未调用):

enter image description here

将它移到外面它应该可以工作。

enter image description here