I would probably make my 'delay' function recursive. Currently your delay
function is 'setLetter' and it is not recursive so you have no real control
over the timeline. It doesn't makes sense to put the recursive logic in your
'setLetter'  so I might have a function named 'process'. Each time it calls
it finds the current context, which message in the array, which point in the
message etc. These values are validated in the same way you do so now.
setLetter is called, auto increments are set and delay is set on 'process'.
The only issue I see is the way you calculate your delay with the variance,
it appears that it would be possible that a position after one could show up
before. I would assume this would create some wierd effects. This could be
prevented if you create some kind of array for the current message to see if
a letter has been set or not.

I hope this helps. If this isn't clear enough please let me know where I can
clarify.

Nathan

On Mon, Sep 29, 2008 at 4:34 PM, liam.smart <[EMAIL PROTECTED]> wrote:

>
> Hi all,
>
> Im new to MooTools and Javascript but I have accomplished a fair bit
> in a short time. One thing I'm not really sure how to do, is loop a
> function repeatedly. I have modified the code at the bottom to try and
> get a looping version.
>
> I am trying to create a news ticker like on the BBC website. Just now,
> it just displays the last instance in the array, but I need to keep
> cycling through all the instances in the array, and then start from
> the start again.
>
> I have thought about getting the length of the string, multiplying
> that by the speed, and then doing a set-interval to call the function,
> but the speed varies so that option will not work.
>
> I then thought about doing a for loop and when x == the length of the
> array, start the function again. But the ticker is just reading the
> last instance.
>
> I don't need some-one to do this for me, but just need pointed in the
> right direction. Should I be using the .delay() method, or is
> the .periodical() one the one I want? Do I need to create a chain? Its
> a fair bit of code to post so will just post the important bits
> (hopefully people can see what I'm trying to do here. After it has
> went through the array, start again, but obviously the loop is
> finishing quicker than the actual ticker, so that what makes me think
> I need to chain the functions and set delays...):
>
> [start code]
> //initialization
> initialize: function(options)
> {
>        //set options
>        this.setOptions(options);
>
>        //remove 'Hidden' class
>        if(this.options.container.hasClass('Hidden'))
>        {
>                this.options.container.removeClass('Hidden');
>        };
>
>        //retrieve/split/set new html
>        this.options.initString = this.options.container.innerHTML
>        this.options.splitString = this.options.initString.split(',');
>        this.options.container.set('html','<span>Latest:</span>');
> },
>
> //start the blogTicker
> start: function()
> {
>        //set new news items from split string
>        this.options.message =
> this.options.splitString[this.options.counter];
>
>        //for every letter
>        for(x = 0; x < this.options.message.length; x++)
>        {
>                //spits out characters at random pace
>                var pace = (this.options.delay * x) +
> $random(0,this.options.variance);
>                var current = this.options.message.charAt(x);
>
>                //spit out the letter or delete one if backChar is
> encountered
>                if(current != this.options.backChar)
>                {
>                        var go = this.setLetter.delay(pace,this);
>                }
>                else
>                {
>                        var go = this.deleteLetter.delay(pace +
> this.options.backDelay,this);
>                };
>        };
>
>        if(this.options.counter > this.options.splitString.length-1)
>        {
>                this.options.counter = 0;
>                this.initialize();
>        }
>        else
>        {
>                this.options.counter++;
>        };
> },
>
> //place the newest letter in the container
> setLetter: function()
> {
>        //push string into the links now character by character
>        this.options.container.set('html',this.options.container.get('html')
> + '' + this.options.message.charAt(this.options.cursor));
>
>        //increment cursor
>        this.options.cursor++;
> },
> [end code]
>
> Like I said, I'm not here for a free ride, I just need pointed in the
> right direction as I have seriously spent a lot of time trying
> different things.
>
> Many thanks,
> Liam
>

Reply via email to