使用 jQuery Ajax 获取大量不需要的 GET 请求
这是我目前的工作演示: http://isaaclean.com/test/test2
基本上,如果您在主导航中单击“博客”列,旁边的第二列将 Ajax 加载包含“帖子”列表的“blog.html”。如果您单击第二列中的一篇帖子,第三列将加载该帖子的内容。
虽然这工作正常,但如果您再次单击“博客”,脚本就会变得混乱。 Firebug 在第二列上显示了超过 20 个 GET 请求。然后,当您单击第二列中的链接时,将发送等量的 GET 请求。你甚至可以看到动画变得疯狂。我不知道为什么会发生这种情况。
当我将以下行添加到 mainnav.js 时,它开始发生:
if($('#listnav').children().size() == 1) {
$('#listnav nav li').stop().hide("slide", {direction: "left"}, loadContent);
} else {
$('#listnav').load(toLoad, '', showNewContent);
}
原因我添加此内容的原因是,当第二次单击“博客”时,我想在脚本 Ajax 加载另一个 blog.html 之前显示完整的滑出动画。在我实现上面的代码和loadContent函数之前,问题是滑出动画被页面加载切断。为了防止这种情况,我认为使用回调函数会起作用,因为回调是在动画完成后发生的。
唯一的问题是,如果您第一次加载页面,#listnav 没有子元素。因此,如果您尝试使用以下代码:
$('#listnav nav li').stop().hide("slide", {direction: "left"}, loadContent);
...回调函数永远不会执行,因为在您单击“博客”之前没有 nav li 元素。这就是为什么我尝试实现一个 if 语句来检查 #listnav 中是否有元素,并且它在第一次单击时工作正常,但脚本后的任何单击都会出错。
那么有人可以帮我找到这个问题的解决方案吗?
Here's a demo of the work I have at the moment:
http://isaaclean.com/test/test2
Basically if you click on "Blog" in the main navigation column, a second column next to it will Ajax loads "blog.html" that contains a list of "posts." If you click on one of the posts in the second column, a third column will load in with the content of the post.
While that's working fine, if you click "Blog" again, this is where the script goes haywire. Firebug shows over 20 GET requests on the second column. And then when you click on a link in the second column after that, an equal amount of GET requests is sent. You can even see the animations going crazy. I have no idea why this is happening.
It started to occur when I added the following lines to mainnav.js:
if($('#listnav').children().size() == 1) {
$('#listnav nav li').stop().hide("slide", {direction: "left"}, loadContent);
} else {
$('#listnav').load(toLoad, '', showNewContent);
}
The reason why I added this was because I wanted to show the full slide out animation before the script Ajax loads another blog.html when "Blog" is clicked for the second time. Before I implemented the code above and loadContent function, the problem was that the slide out animation was cut off by the loading of the page. To prevent this, I thought using a callback function would work since callbacks occur after the animation is completed.
The only issue with this is that if you first load the page #listnav has no child elements. Therefore if you try using the following code:
$('#listnav nav li').stop().hide("slide", {direction: "left"}, loadContent);
...the callback function is never executed since there are no nav li elements until you click "Blog." That's why I tried to implement an if statement which checks if there are elements within #listnav, and it works fine on the first click, but any clicks after the script just bugs out.
So can anyone help me find a solution to this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的问题是 mainnav.js 中的 loadcontent() 对其动画的每个节点运行一次。我不知道你所看到的效果到底是如何最终发生的,但这肯定是根本原因。
我目前无法测试这个,所以这是未经测试的,但是沿着这些思路应该可以工作:
请注意,loadcontent 不再是初始动画的一部分,而是仅在最后一个节点被隐藏后执行...尽管,我不确定如果这段代码确实有效,因为我找不到简单地对自定义函数进行排队的方法。我不喜欢 jQuery 效果。
但当我想一想,也许这只是一个更好、更稳健的解决方案:
I believe your issue is that loadcontent() in mainnav.js is run once for EVERY node it animates. I'm not exactly how the effect you see really ends up happening, but that surely is the root cause.
I can't test this currently so this is untested, but something along these lines should work:
Note that loadcontent is no longer part of the initial animation but is only executed once the last node is hidden... although, I'm unsure if this code actually works as I can't find way to simply queue a custom function. jQuery effects is not my cup of tea.
But when I think about it, perhaps this is simply a better and more robust solution: