开发在线客服系统聊天界面,JS实现相邻两条消息之间间隔小于3分钟,就不展示消息时间

我在开发一个客服聊天界面,每条消息都有发送时间,我想要实现相邻两条消息之间间隔小于3分钟的就不展示发送时间.

下面就是消息的JSON结构,里面的time就是发送时间

message = {
  "msg_id": 629255,
  "time": "2023-02-11 00:41:04",
  "content": "chatGPT",
  "mes_type": "visitor",
  "name": "山东聊城",
  "avator": "/static/images/computer.png",
  "read_status": "read"
};

下面的msgList就是消息的数组,当前 i 的时间,与 i+1 的时间取差值,小于3分钟的,就把show_time设置为false

这样在界面上渲染,循环数组的时候,判断show_time来决定是否展示时间

for(var i=0;i<msgList.length;i++) {if((i+1)<msgList.length){
                            const nextTime = new Date(msgList[i+1]["time"]).getTime();
                            const currTime = new Date(row["time"]).getTime();
                            if((currTime-nextTime)>=180000){
                                row.show_time=true;
                            }else{
                                row.show_time=false;
                            }
                        }
                        _this.msgList.unshift(row);
                    }