JS脚本执行缓慢 [英] Slow execution of JS script
本文介绍了JS脚本执行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的脚本来切换 onmouseover 上的帖子图片/文字。如果帖子数不超过2个,则代码可以正常工作,但当帖子数量增加时,代码变得非常慢。
I have a simple script to toggle post image/text on onmouseover. The code works fine if there are no more than 2 posts, but it becomes VERY slow when the number of posts increases.
function showPost(ele, eve) {
var id = ele.id.replace("post-", "");
if (typeof window['timerHide-' + id] !== "undefined") {
clearInterval(window['timerHide-' + id]);
}
var target = (eve.relatedTarget) ? eve.relatedTarget : eve.toElement;
var parent = $("post-" + id);
if (typeof target === "undefined" || target === parent || target.parentNode === parent || target.parentNode.parentNode === parent) {
return;
}
var img = $("post-img-" + id);
var txt = $("post-txt-" + id);
if (img.style.opacity === "") {
img.style.opacity = 1;
}
var opacity = img.style.opacity;
window['timerShow-' + id] = setInterval(function () {
if (opacity <= 0.1) {
clearInterval(window['timerShow-' + id]);
img.style.display = 'none';
txt.style.display = 'block';
txt.style.opacity = 1;
}
img.style.opacity = opacity;
img.style.filter = 'alpha(opacity=' + opacity * 100 + ")";
opacity -= opacity * 0.1;
}, 1);
};
除了隐藏切换文本外,其他函数几乎与此函数完全相同。
The other function is almost identitcal to this one except for that it hides the toggled text.
Thx
推荐答案
您似乎将间隔ID保存到DOM元素,尝试使用变量
You seem to be saving the interval ID to a DOM element, try using a variable
var timerID = setInterval(function ()
if (opacity <= 0.1)
{
clearInterval(timerID);
这篇关于JS脚本执行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文