如何在Vue方法中取消setTimeout?

问题描述 投票:0回答:2

我在v-on:mouseover =“ showOverlay”]中使用setTimeout触发了覆盖。我的工作很好。

        showOverlay() {
            this.timeout = setTimeout(() => {
                this.overlay = true
            }, 1000)
        },

我想取消鼠标释放的超时。我在v-on:mouseleave =“ hideOverlay”中尝试了clearTimeout。

        hideOverlay() {
            clearTimeout(this.timeout)
        },

这是我的模板:

        <a v-on:mouseover="showOverlay" v-on:mouseleave="hideOverlay">
            something...
        </a>

        <v-overlay :value="overlay" v-on:mouseover="overlay = false">
            something...
        </v-overlay>

我在v-on:mouseover =“ showOverlay”中使用setTimeout触发了叠加层。我很好showOverlay(){this.timeout = setTimeout(()=> {this.overlay = true ...

vue.js settimeout mouseover mouseleave
2个回答
0
投票

如果您在1000毫秒之前mouseleave,超时将被清除,并且覆盖层不会显示,它可以正常工作。如果您还想在鼠标离开时隐藏叠加层,请执行以下操作:


0
投票

我找到了这样的解决方案。我添加了超时条件。但这是一个干净的解决方案吗? idk。

© www.soinside.com 2019 - 2024. All rights reserved.