jQuery fadeTo 回调无明显原因循环

发布于 2024-11-24 04:32:40 字数 699 浏览 1 评论 0原文

下面的代码有效,但由于某种原因 $('#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2024-12-01 04:32:40

每次运行 fullViewButton() 时,它都会向 'input#fullChart' 元素分配另一个相同的 .click() 处理程序。

因此,当实际单击(或触发)时,它会运行迄今为止已分配的处理程序的累积。

您应该仅将 .click() 处理程序分配给 'input#fullChart' 一次。

不确定你的其他代码是什么样的,但你可能想要这样的东西:

   // This assigns the click handler
$('input#fullChart').click(function(){
    $('#chart').fadeTo(400,0, function(){
        showFullChart();
    });
    $('#chart').fadeTo(400,1);
});

function fullViewButton(zoom){
if (zoom == 0){
    $('input#fullChart').hide();
} else {
    $('input#fullChart').show().click();  // <-- this invokes the click handler

}

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:

   // This assigns the click handler
$('input#fullChart').click(function(){
    $('#chart').fadeTo(400,0, function(){
        showFullChart();
    });
    $('#chart').fadeTo(400,1);
});

function fullViewButton(zoom){
if (zoom == 0){
    $('input#fullChart').hide();
} else {
    $('input#fullChart').show().click();  // <-- this invokes the click handler

}
十年不长 2024-12-01 04:32:40

哈,无法告诉你我已经这样做了多少次,原因是你多次调用 fullViewButton ,并且每次都重新附加 fadeTo 函数'点击'。

为了快速修复,您可以将其放在

$('input#fullChart').click(function(){
    $('#chart').fadeTo(400,0, function(){
        showFullChart();
    });
    $('#chart').fadeTo(400,1);
});

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 the fadeTo function on 'click'.

for a quick fix you can just put

$('input#fullChart').click(function(){
    $('#chart').fadeTo(400,0, function(){
        showFullChart();
    });
    $('#chart').fadeTo(400,1);
});

outside of fullViewButton()

大姐,你呐 2024-12-01 04:32:40

尝试将 stop() 方法与 animate 一起使用,如下所示:

function fullViewButton(zoom){

var fChart = $('input#fullChart');
var chart = $('#chart');

if (zoom == 0){
    fChart.css({display:'none',opacity:0});
} else {
    fChart.css({display:'block',opacity:1});
    fChart.click(function(){
        chart.stop().animate({opacity:0}, 400, function(){
            $(this).css({display: 'none'});
            showFullChart();
        });
        chart.stop().css({display:'block',opacity:0}).animate({opacity:1}, 400);
    });
}

Try using the stop() method along with animate, like so:

function fullViewButton(zoom){

var fChart = $('input#fullChart');
var chart = $('#chart');

if (zoom == 0){
    fChart.css({display:'none',opacity:0});
} else {
    fChart.css({display:'block',opacity:1});
    fChart.click(function(){
        chart.stop().animate({opacity:0}, 400, function(){
            $(this).css({display: 'none'});
            showFullChart();
        });
        chart.stop().css({display:'block',opacity:0}).animate({opacity:1}, 400);
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文