【问题标题】:understanding javascript code with callbacks用回调理解 javascript 代码
【发布时间】:2011-05-29 14:02:15
【问题描述】:

下面是twich.menode.js 聊天的部分服务器端代码:

exports.channel = function(MESSAGE_BACKLOG, MESSAGE_TRUNCATE) {
    return (function() {
        var messages  = [],
            callbacks = [];

        return {
            appendMessage : function (nick, room, type, text) {

  //truncate message if necessary
  if (type == 'msg' && text.length > MESSAGE_TRUNCATE) {
   text = text.substr(0, MESSAGE_TRUNCATE) + "... (trunc.)";
  }

  //message
  var m = {
   nick: nick,
   type: type, // "msg", "join", "part"
   text: text,
   room: room,
   timestamp: (new Date()).getTime()
  };

  //output to console
//  mlog(m);

  //push msg on message stack
  messages.push( m );

  //???
                while (callbacks.length > 0) {
                    callbacks.shift().callback([m]);
                }

  //old messages get pushed out of message stack
                while (messages.length > MESSAGE_BACKLOG) {
                    messages.shift();
                }
            },

            query : function (room, since, callback) {
                var matching = [];
                for (var i = 0; i < messages.length; i++) {
                    var message = messages[i];
                    if (message.timestamp > since && room == message.room) {
                        matching.push(message)
                    }
                }

    //???
                if (matching.length != 0) {
                    callback(matching);
                }
                else {
                    callbacks.push({ timestamp: new Date(), callback: callback });
                }
            },

     //run initially when script starts
            init : function() {
                // clear old callbacks older than 25 seconds (lowered from 30 seconds to get round rmit proxy server's 30sec timeout
                setInterval(function () {
                    var now = new Date();
                    while (callbacks.length > 0 && now - callbacks[0].timestamp > 25*1000) {
                        callbacks.shift().callback([]);
                    }
                }, 3000);
                return "hi";
            }
        }
    }());
}

代码负责从其中一个聊天室存储和检索聊天消息。 我不是 javascript 程序员。我的背景是 PHP,一切都是程序化的。我想用 memcached 来解决这个问题。但首先我需要了解到底发生了什么。我添加了额外的 cmets。我不明白的是所有带有回调的东西。你能帮我理解回调在做什么吗?

【问题讨论】:

    标签: javascript node.js chat


    【解决方案1】:

    我真的不明白你想要什么,但这是发生了什么:

                while (callbacks.length > 0) {
                    callbacks.shift().callback([m]);
                }
    

    当数组回调中的对象数量大于0时, callbacks.shift() 函数显然会返回一个对象,该对象具有一个名为 callback 的属性,该属性是一个函数。它使用一个包含变量 m 的数组调用该函数。

                if (matching.length != 0) {
                    callback(matching);
                }
                else {
                    callbacks.push({ timestamp: new Date(), callback: callback });
                }
            }
    

    如果匹配的数组中的对象数量不为0,则调用函数回调,如果是,则使用对象调用函数callback.push。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2015-03-10
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      相关资源
      最近更新 更多