views:

286

answers:

2

Hey,

I have some jQuery going here:

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

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

    var randomNum = Math.floor(Math.random()*3); /* Pick random div to show */
    $('div#ticker div:eq(' + randomNum + ')').show();

    $("#ticker").animate({right: "0"}, {duration: 800 });

});

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

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

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

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

    });

});

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

    var randomNum = Math.floor(Math.random()*3);
    $('div#ticker div:eq(' + randomNum + ')').show();

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

    $("#ticker").oneTime(20000,function(i) { /* Retract once */

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

    });

    $("#ticker").oneTime(21000,function(i) { /* Hide all divs once */

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

    });

});

I am trying to get it to show a random div the first time it pulls out, which has been accomplished in the first part of the code. But after that right now I have it grabbing a random div (out of 3 divs) everytime after that too. I need to change this part of the code so it looks at the list of 3 divs and goes to the next one down every time.

So if the first time the div that pulled out was 2 then every time after that would go in this order: div 3, div 1, div 2, div 3, div 1 etc.

Here is a demo of what is going on right now: treethink.treethink.net/backup

Thanks, Wade

A: 

instead of generating a new random number every time, generate it first then increment it with a mod 3, that way you will get the next divs in order.

for example, if your random number generated 2, the next one would be 3%3 = 0, then 1%3 = 1 then 2%3 = 2 and so on.

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

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

randomNum = (randomNum+1)%3;

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


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


$("#ticker").oneTime(20000,function(i) { /* Retract once */

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

});

$("#ticker").oneTime(21000,function(i) { /* Hide all divs once */

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

});
});
John Boker
Thanks a lot. I'm just having one issue now actually with this. The first 2 are the same, so the first one will be a random number, next will eb that same number then after it will go one up. I changed $('div#ticker div:eq(' + (randomNum++%3) + ')').show(); to $('div#ticker div:eq(' + (randomNum+1) + ')').show(); and it fixed that but then after that it won't show the next one just stay on the same one.
Wade D Ouellet
if you break it into two lines it may fix your problem, the problem is it's not actually updating the randomNumber with the randomNum+1
John Boker
How would I break that up? Sorry, I'm very new to jQuery.
Wade D Ouellet
I've modified the answer to show what I'm thinking.
John Boker
Still the same problem unfortunately. Here is my code right now: treethink.treethink.net/backup/jquery.js I think it has something to do with my initial oneTime function that does the first one up front?
Wade D Ouellet
i didnt notice you were showing it the first time in another function, updating the code. just moved the randomNum increment to the line before the one showing the div.
John Boker
Not sure why I didn't realize that, makes total sense. Thanks a lot, take care John!
Wade D Ouellet
A: 

Try this.

$(document).ready(function() {
    var tickers = [
        $('#ticker1'),
        $('#ticker2'),
        $('#ticker3'),
        ];
    for (var i in tickers) tickers[i].hide();

    var cur = Math.floor(Math.random()*3);

    nextslide();

    function nextslide() {
        var ticker = tickers[cur];
        if (cur++=>tickers.length) cur=0;

        ticker.fadeIn(300, function() {
            setTimeout(2000, function(){ 
                // note the (pseudo) recursion, calling nextslide
                ticker.fadeOut(300, nextslide);
            });
        });
    }
});

I have not tested it. I'd avoid the modulo trick: it needs explanation, and you lose your current position if you add extra slides.

mk