Jquery在回调淡出淡出不持久

Jquery fadeIn in callback for fadeOut not lasting

本文关键字:淡出 不持久 回调 Jquery      更新时间:2023-09-26

希望有人能对这个问题给我一些启示....我使用setInterval在网页上交替显示标题。它会淡出前一个,然后在回调函数中淡出新一个。它曾经工作得很好,但是我把回调函数从淡出中分离出来,因为我想在没有延迟的情况下运行它,现在我得到了最初的标题,但是当需要改变的时候,它们会在一瞬间淡出,然后再次消失。

       function processSidebar(data) {
            var headlines = $.parseJSON(data);
            var sIndex = 0;
            function newSidebar(surl, sIndex) {
                $(".sidebar").html(headlines[sIndex].date + '<br><a href="' + surl + '">' + headlines[sIndex].line + '</a>');
                $(".sidebar").fadeIn(400);
            }
            newSidebar("getme.php?blog=1&headline=1", sIndex);
            setInterval(function() {
                ++sIndex;
                if (sIndex == headlines.length) {sIndex = 0}
                var surl="getme.php?blog=1&headline=" + headlines[sIndex].index;
                $(".sidebar").fadeOut(400,newSidebar(surl,sIndex));
            }, 10000); // end setInterval
        }; // end processSidebar

jQuery的fadeOut需要一个函数作为complete的参数。

你给newSidebar(surl,sIndex),它立即得到评估,不返回任何东西(但做整个fadeIn的东西)。

你想使用一个匿名函数:

$(".sidebar").fadeOut(400年,函数(){newSidebar (surl sIndex)});