views:

345

answers:

1

Hey,

I have my site going here: http://www.treethink.com

I am trying to make it so when a navigation item is clicked, it retracts the news ticker on the right and then doesn't run any of the extracting/timer functions anymore. I got it to retract but it still extracts.

How I am doing it is I am adding a "noTicker" class with the nav buttons and removing it with colorbox's close button. The function runs on the page initially and if there isn't a "noTicker" class it runs the news ticker as planned. When a nav or close button is clicked it runs the function again which checks again to see if it has that class.

So if it has the class it should retract (which it is so that must mean it's adding the class properly) and then not run any of the timer functions, but it still runs the timer functions for some reason. Here is my jQuery. It is also not removing the class properly when the close button is clicked, according to firebug.

/* 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();

});

Thanks,

Wade

+1  A: 

It doesn't look like your everyTime() timer will get cancelled when you add the noTimer class to your #ticker div. The first call to newsTicker() will presumably set this going, however, I don't see that subsequent calls will stop it.

If I am reading the correct documentation, you can give your timer a (string) label, then to stop it you can call stopTime() and supply the label.

Here is the documentation I am working from: http://plugins.jquery.com/project/timers

fd
That makes sense, I wonder how I can end the timer.
Wade D Ouellet
Updated with the info.
fd
I added $("#ticker").stopTime(); to the else statement which worked, but the removeClass still isn't working for the close button so it isn't showing up again after colorbox is closed.
Wade D Ouellet
Are you sure the callback is triggering? Try putting a quick alert() in to check it. If it is, the only other thing I can think of is that somehow the "#nav li" callback is also being triggered after it.
fd
Well according to firebug it's not removing the class at all and I'm not sure what a quick alert is?
Wade D Ouellet
how do I use the quick alert to check this? I'm not familiar with it?Thanks for your help with figuring out the first issue, this one definitely needs to be solved too though.
Wade D Ouellet
I'm gonna post a new topic because it's kind of a separate issue. I appreciate your help for the first problem though so I gave you a check, thanks a lot.
Wade D Ouellet
I meant that you could add a call to the alert() function inside your callback to show whether it is being called. For example, alert("Callback was activated"); added inside the callback function (temporarily, of course).
fd
where does the alert show up if so? I'm not seeing anything when I put it inside the #cboxClose click function.
Wade D Ouellet
It seems that no matter what link I attach the removeClass to it won't work, I tried attaching an addClass to there as well and it wouldn't work. Which means the problem isn't the link and isn't the removeClass...It's something else.
Wade D Ouellet
The alert() should open a modal dialog box, you won't miss it if it happens. This implies your callback is not being executed. You need to check that the $("#cboxClose") is correctly selecting the element you want it to. Is there an element with this id in your HTML? Does the id match exactly? Is there some way in which clicks to this element might be failing; eg is it covered by another (transparent) element?
fd