JS脚本执行缓慢

编程入门 行业动态 更新时间:2024-10-11 01:19:31
本文介绍了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脚本执行缓慢

本文发布于:2023-11-23 13:25:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   缓慢   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!