Jquery hasClass有条件不起作用

时间:2010-03-28 23:32:53

标签: jquery class function conditional addclass

我试图这样做,当点击导航项时,它会缩回右侧的新闻自动收报机,然后不再运行任何提取/计时器功能。我让它收回但它仍然提取。

我是如何做的是我添加了一个带有导航按钮的“noTicker”类,并使用colorbox的关闭按钮将其删除。该函数最初在页面上运行,如果没有“noTicker”类,则按计划运行新闻自动收报机。单击导航或关闭按钮时,它会再次运行该功能,再次检查该功能是否具有该类。

因此,如果它有类,它应该收回(这是因为它必须意味着它正确地添加类)然后不运行任何定时器函数,但它仍然由于某种原因运行定时器函数。这是我的jQuery。根据萤火虫的说法,单击关闭按钮时也没有正确删除类。

/* Initially hide all news items */

$('#ticker1').hide();
$('#ticker2').hide();
$('#ticker3').hide();

var randomNum = Math.floor(Math.random()*3); /* Pick random number */

newsTicker();

function newsTicker() {

    if (!$("#ticker").hasClass("noTicker")) {

        $("#ticker").oneTime(2000,function(i) { /* Do the first pull out once */

            $('div#ticker div:eq(' + randomNum + ')').show(); /* Select div with random number */

            $("#ticker").animate({right: "0"}, {duration: 800 }); /* Pull out ticker with random div */

        });

        $("#ticker").oneTime(15000,function(i) { /* Do the first retract once */

            $("#ticker").animate({right: "-450"}, {duration: 800}); /* Retract ticker */

            $("#ticker").oneTime(1000,function(i) { /* Afterwards */

                $('div#ticker div:eq(' + (randomNum) + ')').hide(); /* Hide that div */

            });

        });

        $("#ticker").everyTime(16500,function(i) { /* Everytime timer gets to certain point */

            /* Show next div */

            randomNum = (randomNum+1)%3;

            $('div#ticker div:eq(' + (randomNum) + ')').show();

            $("#ticker").animate({right: "0"}, {duration: 800}); /* Pull out right away */


            $("#ticker").oneTime(15000,function(i) { /* Afterwards */

                $("#ticker").animate({right: "-450"}, {duration: 800});/* Retract ticker */

            });

            $("#ticker").oneTime(16000,function(i) { /* Afterwards */

                /* Hide all divs */

                $('#ticker1').hide();
                $('#ticker2').hide();
                $('#ticker3').hide();

            });

        });

    } else {

        $("#ticker").animate({right: "-450"}, {duration: 800}); /* Retract ticker */

        $("#ticker").oneTime(1000,function(i) { /* Afterwards */

            $('div#ticker div:eq(' + (randomNum) + ')').hide(); /* Hide that div */

        });

    }

}

/* when nav item is clicked re-run news ticker function but give it new class to prevent activity */

$("#nav li").click(function() {

    $("#ticker").addClass("noTicker");

    newsTicker();

});

/* when close button is clicked re-run news ticker function but take away new class so activity can start again */

$("#cboxClose").click(function() {

    $("#ticker").removeClass("noTicker");

    newsTicker();

});

谢谢,

瓦德

1 个答案:

答案 0 :(得分:1)

将noTimer类添加到#ticker div时,看起来你的everyTime()计时器不会被取消。第一次调用newsTicker()可能会设置这个,但是,我没有看到后续调用会阻止它。

如果我正在阅读正确的文档,您可以给定时器一个(字符串)标签,然后停止它,您可以调用stopTime()并提供标签。

以下是我正在处理的文档: http://plugins.jquery.com/project/timers