setTimeouts 运行后我是否必须清除它们?

发布于 2024-11-16 12:03:14 字数 286 浏览 3 评论 0原文

确实是一个简单的问题,我正在运行一堆超时,但想确保它们不会减慢页面速度,并且由于某种原因,它们在执行后不会保留在内存中。

$projects.each(function(index) {
    var $this = $(this);
    window.setTimeout(function() {
        // animate
    }, 300 * index);
});
// Clear timeouts?

我的猜测是,它们一旦运行就会被销毁,但只想遵循最佳实践。

Simple question really, I'm running a bunch of timeouts but wanna make sure they don't slow the page down and that for some reason they aren't kept in memory after they've executed.

$projects.each(function(index) {
    var $this = $(this);
    window.setTimeout(function() {
        // animate
    }, 300 * index);
});
// Clear timeouts?

My guess is that they're destroyed once they've run but just want to follow best practice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-11-23 12:03:15

不,你不知道。间隔计时器(通过“setInterval()”),是的,如果您希望它们停止的话。

清除不需要清除的超时是无害的。也就是说,如果您在运行后确实清除它,浏览器不会抱怨。

No, you don't. Interval timers (via "setInterval()"), yes, if you want them to stop.

It's harmless to clear a timeout that doesn't have to be cleared. That is, if you do clear one after it has run, browsers won't complain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文