new Date().getTime() 在JavaScript中显示NaN

问题描述 投票:0回答:1

我试图在时间戳键内的一个临时对象中使用 new Date.getTime() 并将其推送到一个数组中,这个数组位于一个全局访问的变量中。GLOBAL_VAR.但在我创建临时变量并打印其值后,它显示时间戳是 NaN 我不知道为什么我不能把它放在对象里面(该对象的)。console.log() 而结果如下图所示)

还有:handleOrderMessage 函数在收到socket.io事件时被调用,我不知道这是否会影响到 new Date.getTime() 函数,显示输出。

console.log("TEMP OBJECT IS", tempObject)
/*
{
id: "-909e-11ea-9ede-066a42ffe1ae",
​price: "0.1",
​quantity: "0.1",
​side: "buy",
​timestamp: NaN
}
*/
    function handleOrderMessage(msg) {


        let d = new Date();
        console.log(d.getTime());
        if (msg['status'] === "PLACED") {
            //for buy
            if (msg['orderSide'] === "Buy") {
                let tempObject = {
                    side: "buy",
                    id: msg["OrderID"],
                    timestamp: d.getTime(),
                    quantity: parseFloat(msg["Quantity"].toFixed(4)).toString(),
                    price: parseFloat(msg["Price"].toFixed(4)).toString()
                }
                //pushing to the global orders with price from msg as key
                if (tempObject.price in GLOBAL_VAR.bidOrdersPricesAsKey) {
                    GLOBAL_VAR.bidOrdersPricesAsKey[tempObject.price].push(tempObject);
                } else {
                    GLOBAL_VAR.bidOrdersPricesAsKey[tempObject.price] = [tempObject]
                }
                console.log("GLOBAL VAR BUY ORDERS PRICES ASK KEY", GLOBAL_VAR.bidOrdersPricesAsKey)
                console.log("TEMP OBJECT IS", tempObject)
      }
    }
javascript html nan gettime
1个回答
1
投票

下面的操作似乎很好,不知道为什么你会得到NaN。请提供完整的工作代码来解决这个问题。

doit();
function doit() {
  let d = new Date();
  let tempObject = {
    side: "buy",
    timestamp: d.getTime()
  };
  console.log(tempObject);
}
© www.soinside.com 2019 - 2024. All rights reserved.