views:

225

answers:

1

Hello,

I have a content area that loops through divs and shows there content.

I'm having trouble making it display the initial content, unfortunately it waits 5000 milliseconds before triggering the very first content area to display.

Anyone spot an easy way to make it display the initial area, then slide to the next area and do them at 5000 milliseconds.

JS

Model.FeatureBar = {
current:0,
items:{},
init: function init(options){
    var me = this;
    me.triggers = []
    me.slides = []
    this.container = jQuery('#features');
    jQuery('.feature').each(function(i){
        me.triggers[i] = {url: jQuery(this).children('.feature-title a').href, title: jQuery(this).children('.feature-title'),description:jQuery(this).children('.feature-description')}
        me.slides[i] = {image: jQuery(this).children('.feature-image')}
    });

    for(var i in this.slides){
        this.slides[i].image.hide();
        this.triggers[i].description.hide();
    }

    setInterval(function(){Model.FeatureBar.next()},5000);
},
next: function next(){
    var i = (this.current+1 < this.triggers.length) ? this.current+1 : 0;
    this.goToItem(i);
},
previous: function previous(){
    var i = (this.current-1 > 1) ? this.current-1 : this.triggers.length;
    this.goToItem(i);
},
goToItem: function goToItem(i){
    if(!this.slides[i])
        throw 'Slide out of range';
    this.triggers[this.current].description.slideUp();
    this.triggers[i].description.slideDown();

    this.slides[this.current].image.hide();
    this.slides[i].image.show();

    this.current = i;
},

}

html

<div id="features">
<div class="feature current">
<div class="movie-cover">
<a href="/police/movie"><img title="" alt="" src="/design/images/four-oh-four.png"></a>
</div>
<div class="feature-image" style="display: none;">
<img src="/design/images/four-oh-four.png">
</div>
<h2 class="feature-title"><a href="/police/movie">Police</a></h2>
<p class="feature-description" style="overflow: hidden; display: block; height: 50.8604px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;">DESCRIPTION</p>
</div>

<div class="feature">
<div class="movie-cover">
<a href="/rude/movie"><img title="" alt="" src="/design/images/four-oh-four.png"></a>
</div>
<div class="feature-image" style="display: none;">
<img src="/design/images/four-oh-four.png">
</div>
<h2 class="feature-title"><a href="/rude/movie">Rude</a></h2>
<p class="feature-description" style="overflow: hidden; display: block; height: 18.3475px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;">DESCRIPTION</p>
</div>

<div class="feature">
<div class="movie-cover">
<a href="/brits/movie"><img title="" alt="" src="/design/images/four-oh-four.png"></a>
</div>
<div class="feature-image" style="display: block;">
<img src="/design/images/four-oh-four.png">
</div>
<h2 class="feature-title"><a href="/brits/movie">Brits</a></h2>
<p class="feature-description" style="overflow: hidden; display: block; height: 40.1549px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;">DESCRIPTION</p>
</div>

<div class="feature">
<div class="movie-cover">
<a href="/indie/movie"><img title="" alt="" src="/design/images/four-oh-four.png"></a>
</div>
<div class="feature-image" style="display: none;">
<img src="/design/images/four-oh-four.png">
</div>
<h2 class="feature-title"><a href="/indie/movie">Indie</a></h2>
<p class="feature-description" style="overflow: hidden; display: block; height: 42.4247px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;">DESCRIPTION</p>
</div>

+2  A: 

In your init function when setting the interval, just call it once immediately as well, like this:

setInterval(function(){Model.FeatureBar.next()},5000);
Model.FeatureBar.next();

Also, if you're calling a function without any parameters, you can pass just the function, no need for an anonymous method wrapping it, like this:

setInterval(Model.FeatureBar.next,5000);
Model.FeatureBar.next();
Nick Craver
Flawless answer, too bad I'm out of votes :(You get a virtual +1.
GlenCrawford
This doesnt fix it.Also note that its called at the end of the int already.
azz0r
@azz0r - You're setting the **interval** at the end of the init...but I don't see a `Model.FeatureBar.next();` in there, my answer has 2 lines your your `init`, you currently have 1 :)
Nick Craver
Okay so I now have: Model.FeatureBar.next(); setInterval(function(){Model.FeatureBar.next()},2000);It starts instantly now, but it starts on the second slide, not the first. hm
azz0r
edit, I've just done: Model.FeatureBar.goToItem(0); instead :)
azz0r
@azz0r - Easiest fix is to set `current--` or `current: -1` up top.
Nick Craver