jQuery fadeTo 回调无明显原因循环
下面的代码有效,但由于某种原因 $('#chart').fadeTo 回调似乎创建了一个非常奇怪的循环。
function fullViewButton(zoom){
if (zoom == 0){
$('input#fullChart').hide();
} else {
$('input#fullChart').show();
$('input#fullChart').click(function(){
$('#chart').fadeTo(400,0, function(){
showFullChart();
});
$('#chart').fadeTo(400,1);
});
}
基本上,我正在绘制一个显示总体统计数据的图表。用户可以选择他想要查看图表的子集,代码将在同一 div 中重新绘制图表。在此详细视图中,我弹出一个按钮,让用户返回到整体视图。
该代码一次可以正常工作(从总体图表到详细图表,然后返回总体图表)。第二次,当到达第三步时,它将整个图表淡入,再次淡出,然后淡入。第三次,它执行三次。第四次,四次。等等。我不知道什么会创建这样的循环!
我尝试移动 $('#chart').fadeTo(400,1);在回调中,这最终会使整个图表需要两倍(或三倍或四倍)的时间才能显示。这段代码有问题吗?还是我可能搞砸了 showFullChart 中的某些内容?
The below code works, but for some reason the $('#chart').fadeTo callback seems to create a really weird loop.
function fullViewButton(zoom){
if (zoom == 0){
$('input#fullChart').hide();
} else {
$('input#fullChart').show();
$('input#fullChart').click(function(){
$('#chart').fadeTo(400,0, function(){
showFullChart();
});
$('#chart').fadeTo(400,1);
});
}
Basically, I'm plotting a graph that shows overall statistics. The user can pick a subset he wants to see charted, and the code will re-draw the graph in the same div. In this detail view, I bring up a button that lets the user go back to the overall view.
This code works fine once (going from overall chart to detail chart and back to overall chart). The second time, when it gets to the third step, it fades the overall chart back in, fades it out again, and fades it back in. The third time, it does this three times. The fourth, four times. Etc. I can't figure out what would be creating a loop like this!
I've tried moving $('#chart').fadeTo(400,1); within the callback, which just ends up making the whole chart take two (or three, or four) times longer to show up. Is there something wrong with this code, or have I possibly messed up something in showFullChart?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次运行
fullViewButton()
时,它都会向'input#fullChart'
元素分配另一个相同的.click()
处理程序。因此,当实际单击(或触发)时,它会运行迄今为止已分配的处理程序的累积。
您应该仅将
.click()
处理程序分配给'input#fullChart'
一次。不确定你的其他代码是什么样的,但你可能想要这样的东西:
Each time
fullViewButton()
runs, it's assigning another identical.click()
handler to the'input#fullChart'
element.So when it is actually clicked (or triggered), it runs the accumulation of handlers that have been assigned thus far.
You should assign the
.click()
handler to'input#fullChart'
only once.Not sure what your other code looks like, but you probably want something like this:
哈,无法告诉你我已经这样做了多少次,原因是你多次调用
fullViewButton
,并且每次都重新附加fadeTo
函数'点击'。为了快速修复,您可以将其放在
fullViewButton()
之外ha, cant tell you how many times i've done this, the reason is because you're calling
fullViewButton
multiple times, and each time re-appending thefadeTo
function on 'click'.for a quick fix you can just put
outside of
fullViewButton()
尝试将
stop()
方法与 animate 一起使用,如下所示:Try using the
stop()
method along with animate, like so: