NodeJs 中的倒计时问题

issue with countdown in NodeJs

大家早上好

我在使用 NodeJS 的倒计时代码时遇到问题,我目前正在使用 express,我的代码如下:

 router.get("/", (req, res) => {

    let remaining = setInterval(function () {
        time = 30 - Math.floor((new Date().getTime() / 1000.0) % 30);
        console.log(time);
    }, 1000);

    res.render("index", { title: "Website title", remaining }); //Balise Title
});

我不知道为什么倒计时不起作用,我有这个显示 [object object].

奇怪的是我的倒计时在 console.log 中工作正常...... 感谢您的帮助

试试下面的代码:

 router.get("/", (req, res) => {

    var time;
    let remaining = setInterval(function () {
        time = 30 - Math.floor((new Date().getTime() / 1000.0) % 30);
        console.log(time);
    }, 1000);
   res.render("index", { title: "Website title", time }); //Balise Title
});
const io = require('socket.io')();
// or
const Server = require('socket.io');
const io = new Server();
io.on('connect',(socket)=>{
 setInterval(function () {
     
        let time = 30 - Math.floor((new Date().getTime() / 1000.0) % 30);
        socket.emit('get_time',time)
    }, 1000)
    })

在前端从 SocketIO 导入 io -- https://socket.io/docs/

const socket = io('http://localhost:3000');
  socket.on('get_time', (time)=>{
             console.log(time)
           });