如何在离开 vue 页面时停止每隔 10 秒调用的方法?
当用户离开某个 Vue 页面时,需要停止每隔 10 秒自动调用的方法。以下是实现方法:
在组件中定义一个变量来保存 setInterval 的返回值,如下所示:
mounted() { this.timer = setInterval(() => { setTimeout(this.aaa(),0) }, 10000) },
在 beforeDestroy 生命周期钩子中销毁 setInterval,如下所示:
beforeDestroy() { clearInterval(this.timer) }
这样,当组件销毁时,该方法将停止调用。
立即学习“前端免费学习笔记(深入)”;
以上就是如何停止 Vue 页面离开时每隔 10 秒调用的方法?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号