本文介绍了setInterval()如何影响性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Twitter Bootstrap构建一个Web应用程序作为框架,并且在显示/隐藏工具提示时遇到问题。除了试图找到实际问题的解决方案之外,我对我们在此期间使用的解决方法有疑问:

We are building a web app using Twitter Bootstrap as a framework and have problems showing/hiding tooltips. Apart from trying to find a solution to the actual problem, I have a question regarding the workaround we use in the meantime:

性能方面,这通常是一个坏主意使用setInterval()?

Performance-wise, is it generally a bad idea to use setInterval()?

如何衡量由此造成的性能影响?

How can I measure the performance impact caused by this?

有没有更好的如何不断检查元素的存在并将其删除?

Are there better ways to constantly check for the presence of elements and remove them?

// temporary workaround to remove unclosed tooltips
setInterval(function() {
    if ( $('.tooltip.in').length > 1 ) {
        $('.tooltip').remove();
    }
}, 1000);


推荐答案

如果你真的,真的担心性能,使用实时节点列表可能会更快:

if you're really, really worried about performance, using a live nodelist might be faster :

var tooltipInNodes = document.getElementsByClassName("tooltip in");
var tooltipNodes = document.getElementsByClassName("tooltip");

setInterval(function() {
    if ( tooltipInNodes.length > 1 ) {
        $(tooltipNodes).remove();
    }
}, 1000);

尽管如此,这不太可能产生太大的影响,正如已经提到的那样,使用长的setInterval间隔(第二个很大,4ms很小)不太可能产生任何重大影响。

This is unlikely to make much of a difference though, and as has been mentioned, using setInterval with long intervals (a second is big, 4ms is small) is unlikely to have any major effects.

这篇关于setInterval()如何影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:20