【问题标题】:now.js - Object has no method, but in jquery click event worknow.js - 对象没有方法,但在 jquery click 事件中起作用
【发布时间】:2012-07-13 19:59:05
【问题描述】:

我有一个简单的 nowjs 客户端代码:

$j(document).ready(function() {
    window.now = nowInitialize('http://xxx.yyy:6564');

    now.recvMsg = function(message){
        $j("body").append("<br>" + message);
    }

    $j("#send-button").click(function(){
        now.sendMsg("well done"); //## this work
    });
    now.sendMsg("aaaah"); //## this dont work
});

和服务器端代码:

var server = require('http').createServer(function(req, res){});
server.listen(6564);

var nowjs = require("now");
var everyone = nowjs.initialize(server);

everyone.now.sendMsg = function(message){
   everyone.now.recvMsg(message);
};

并且调用 sendMsg(服务器端函数)在单击事件函数中起作用,但在就绪事件之外则不起作用。为什么?

我什至尝试过这样的事情:

setTimeout('now.sendMsg("aaaah")',1000);

还是不行。

【问题讨论】:

  • 我不知道你的意思,但我设置了它:setTimeout('now.sendMsg("aaaah")',2000);并且工作 2 秒,但不能工作 1 秒。嗯,也许我一直循环到现在。sendMsg 存在

标签: javascript node.js nowjs-sockets


【解决方案1】:

NowJS 需要时间来加载自身,并且在代码的第 2 行和第 11 行之间没有时间这样做。当你打电话时:

now.sendMsg("aaaah"); //## this dont work

NowJS 仍未加载,无法执行这段代码。当你点击一个按钮 NowJS 完全加载并运行——并且可以发送消息。添加回调函数以确保在尝试使用之前设置库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多