*初级小白*~ 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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探