Nate_37774
Nate_37774

Reputation: 31

Javascript - pubnub api

Javascript Newb - PHP programmer - Playing with this interesting API and trying to create function call (syntax needed) for this api.procedure;

pubnub.time(
    function(time){
       console.log(time)
                  }
           );

Goal - Have api procedure assign epoch time - when called (preferably) within a function, to be able to assign to concatenated message

  1. Have tried assigning to var (i.e.; mynewvar = pubnub.time(... and the calling document.write procedure - results in unassigned.

  2. Have tried assigning to a function (i.e.; Function mynewfunction () {pubnub.time(...and then call function results in same.

I am obviously missing some key point - I think this may be due to my poor understanding of the API procedure call and the object-dot-notation. Yes I am a procedural programmer from GWBASIC/PASCAL days. Have a field day colleagues, but be gentle :)

Upvotes: 3

Views: 131

Answers (2)

Geremy
Geremy

Reputation: 2445

I think this is what you are trying to do: You are trying to fetch the current time, and then include it as part of the message you publish? If so, this is how you'd do it:

var myTimeStamp = 0;

pubnub.time(
    function(time){

             pubnub.publish({
                 channel : "hello_world",
                 message : {"myTS":time, "myMsg":"This is my message!"}
             });
    }
);

On the subscriber side, you'd be able to convert that time into the format you prefer.

Is this what you are trying to do?

Upvotes: 1

mrbinky3000
mrbinky3000

Reputation: 4291

var pnub = {
   time: function () {
      return new Date().valueOf(); // Epoch time
   }
};

pnub.time(); // 1397940823979

As a PHP programmer turned JavaScript programmer, JavaScript took some getting used to. Basically, make an object {}. Assign methods to the object. Call the methods.

I highly suggest reading Speaking Javascript by Axel Rauschmayer

Upvotes: 0

Related Questions