*初级小白*~ 2022-03-24 15:29 采纳率: 97.9%
浏览 48
已结题

使用定时器,每次往数组添加更新144个时间值,但是只想要最后144个最新的时间值


<!DOCTYPE html>
<html lang="en">

<head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.27.0/moment.js"></script>
        <title>Document</title>
        <style>
                [v-cloak] {
                        display: none;
                }
        </style>
</head>
<div id="app">

        <h2 v-cloak>{{timeList}}</h2>
        <!-- <button>点击显示时间</button>
    {{time}}-{{weeks}} -->
</div>

<body>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.js"></script>
        <script>
                new Vue({
                        el: "#app",
                        data: {
                                timeList: []
                        },
                        methods: {
                                getNowTime() {
                                        let endDate = new Date();
                                        let splitTime = 5 * 60 * 1000;
                                        let count = 144;
                                        //转为时间戳
                                        let endTime = endDate.getTime();
                                        let mod = endTime % splitTime;
                                        if (mod > 0) {
                                                endTime = endTime - mod;
                                        }
                                        while (count-- > 0) {
                                                let d = new Date();
                                                d.setTime(endTime - count * splitTime);
                                                this.timeList.push(moment(d).format("HH:mm:ss"));
                                        }
                                },
                                timer() {
                                        return setInterval(() => {
                                                setTimeout(() => {
                                                        this.getNowTime()
                                                }, 0)
                                        }, 3000)
                                }
                        },
                        created() {
                                this.getNowTime()
                        },
                        watch: {
                                timeList() {
                                        this.timer()
                                }
                        },
                        destroyed() {
                                clearTimeout(this.timer)
                        }
                })
        </script>
</body>

</html>

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-03-24 15:38
    关注

    splice删掉之前的数据在重新添加

    
                    getNowTime() {
                        this.timeList.splice(0, this.timeList.length);///////////////增加这句
    //...原来的代码
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 创建了问题 3月24日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c