0

请参阅下面的示例:

(function() {
  // Initialize the socket & handlers
  var connectToServer = function() {
    var warbleSocket = new SockJS('http://url.com:5555/warble');

    warbleSocket.onopen = function() {
      clearInterval(connectRetry);
      $('.connect-status')
        .removeClass('disconnected')
        .addClass('connected')
        .text('Connected');
    };

    warbleSocket.onmessage = function(e) {
      $('#warble-msg').text(e.data);
    };

    warbleSocket.onclose = function() {
      clearInterval(connectRetry);
      connectRetry = setInterval(connectToServer, 1000);
      $('.connect-status')
        .removeClass('connected')
        .addClass('disconnected')
        .text('Disconnected');
    };

    // Connect the text field to the socket
    $('.msg-sender').off('input').on('input', function() {
      warbleSocket.send($('.msg-sender input').val()); 
    });
  };
  var connectRetry = setInterval(connectToServer, 1000);



  connectRetry.warbleSocket.send("Hi there");  

})();

我想要的是能够warbleSocket.send从外部访问connectRetry

我怎样才能做到这一点?

4

1 回答 1

0

在 JS 中从 IIFE 公开 API:

...
    return {
        send: warbleSocket.send
    }
})();

http://benalman.com/news/2010/11/immediately-invoked-function-expression/

于 2013-10-03T09:16:27.793 回答