jQuery点击手柄在多次点击后运行缓慢
jQuery click hander slow to run after many clicks
我正在尝试将一个点击处理程序绑定到DOM上的几个元素。单击后,该元素将在阴影框中加载一些新内容。在玩了一段时间代码后,我注意到每次点击都需要越来越长的时间来加载。
我通过禁用除了一个简单的console.log之外的所有点击处理程序的功能来测试这一点。即使在这之后,到第15次点击时,响应也越来越慢。我点击了哪篇帖子,甚至内容已经加载,都无关紧要——在第15次点击这个".shadowbox-link"元素时,速度真的开始变慢了。
这是我的点击处理程序:
$j('#content article .shadowbox-link').bind('click', showShadowboxPost);
进入功能:
var showShadowboxPost = function() {
// Unbind click handler
$j(this).unbind('click', showShadowboxPost);
// Variables for ajax request
var postData = {
'postId': $j(this).attr('data-id'),
'postType': $j(this).attr('data-type'),
'elementId': $j(this).attr('id'),
'prevPost': $j(this).prev().attr('id'),
'nextPost': $j(this).next().attr('id')
};
preFadeIn();
// If content already loaded, avoid ajax request
// The following functions include the fadeIn
if($j(this).children('.single-post').length !== 0) {
preLoadedRequest(this)
} else {
ajaxRequest(postData, this)
}
// Rebind click handler
$j(this).bind('click', showShadowboxPost);
return false;
};
此处为完整文件:http://www.clarkthomasny.com/wp-content/themes/cleanslate/js/shadowbox-post.js
HTML基本上是这样的:
<div id="content">
<div id="articles">
<article class="shadowbox-link"></article>
<article class="shadowbox-link"></article>
<article class="shadowbox-link"></article>
[etc..]
</div>
</div>
以下页面位于:http://www.clarkthomasny.com/
我尝试了几种不同的方式来调试它,但我仍然不确定发生了什么,并认为这一定与将点击处理程序绑定到这么多元素有关?我已经在jQuery工作了几年了,我被难住了,请帮帮我。谢谢
使用本机DOM处理程序
document.getElementById('anchorID').onclick=function(){/* some code */}
对不起!这个错误与我发布的代码无关。另一个文件包含所有链接的点击手柄(我知道这是个坏主意),我不小心绑定了两次。因此积累的缓慢。感谢大家的帮助!
相关文章:
- Xpath运行缓慢的查询
- 具有大型数据集的组件仅在 IE11/Edge 上运行缓慢
- 滚动事件运行缓慢-有没有更轻松的方法
- Html5画布动画在Android上运行缓慢
- JQuery animate() 函数延迟且运行缓慢
- 我的网站运行缓慢,我不知道为什么
- 为什么这段代码在火狐中运行缓慢
- Javascript 在加载 200mb 的新 Images() 后在 Safari / iPad2 中运行缓慢.为什么
- 在IE中导致“脚本运行缓慢”警告
- removeAllRanges在chrome/Alternatives中运行缓慢
- jQuery点击手柄在多次点击后运行缓慢
- html5/javascript游戏在ie8中运行缓慢
- jquery游戏在手机上运行缓慢
- Parse.Cloud在iOS上运行缓慢
- PrimeFaces”;此页面上的脚本导致Internet Explorer运行缓慢“;DataTable出错
- 当我使用这个时,浏览器运行缓慢
- 触发('click')导致“脚本运行缓慢,是否要停止它?”
- Gulp任务运行缓慢
- Angular-bootstrap -反复调用带有bootstrap工具提示的$compile会导致运行缓慢
- 在Mac OS X上运行缓慢