走马灯

2020-10-20  本文已影响0人  lucky_yao
<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="js/vue.min.js"></script>
    </head>

    <body>
        <div id="app">
            <button @click="show">浪起来</button>
            <button @click="stop">别浪</button>
            <p>{{msg}}</p>
        </div>

        <script>
            // 创建 Vue 实例,得到 ViewModel
            var vm = new Vue({
                el: '#app',
                data: {
                    msg: '猥琐发育,别浪~~~!',
                    timer: null
                },
                methods: {
                    show() {
                        if (this.timer == null) {
                            // var that = this; //this指代的是我们的vue
                            // that.timer = setInterval(function() { //在定时器中this指的是window
                            //  let strat = that.msg.substring(0, 1);
                            //  let end = that.msg.substring(1);
                            //  that.msg = end + strat;
                            // }, 500)
                            this.timer = setInterval(() => {
                                let strat = this.msg.substring(0, 1);
                                let end = this.msg.substring(1);
                                this.msg = end + strat;
                            },500)
                        }

                    },
                    stop() {
                        clearInterval(this.timer);
                        this.timer = null;
                    }
                }
            });
        </script>
    </body>

</html>
上一篇下一篇

猜你喜欢

热点阅读