Reputation: 1640
I have a simple nowjs code for client:
$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
});
and code for server-side:
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);
};
And calling sendMsg (server side function) work when it is in click event function, but outside in ready event dont work. Why?
I have even tried things like:
setTimeout('now.sendMsg("aaaah")',1000);
still dont work.
Upvotes: 0
Views: 279
Reputation: 4828
NowJS needs time to load itself and there is no time to do that between 2nd and 11th line of your code. When you call:
now.sendMsg("aaaah"); //## this dont work
NowJS is still not loaded and can't execute this piece of code. When you click on a button NowJS is fully loaded and operation - and can send the message. Add a callback function to make sure that the library is set before trying to use it.
Upvotes: 1