views:

156

answers:

3

I have an image slider that I built for my website, and I have it set to slide every 3 seconds (will be slower after diagnostics, but I don't like waiting 10 seconds to see what's wrong). I also have it set so that before it auto-slides, it checks if the toSlide variable is set to 1 (default) or not. When the user clicks on a next/previous link or to a certain slide, it sets toSlide to 0.

My problem is that it auto-slides again, then sets toSlide back to 1, and I can't figure out why. Could anybody help me please?

You can see the HTML on my site, and here is the Javascript:

//Featured Work Image Slider
// sC = sliderCount
// sA = slideAmount
// pH = pictureHeight
// sT = slideTime
var sC = 1;
var pH = 364;
var sT = 364
var toSlide = 1;
function slide(ms) {
    $('#featured-box li').stop().animate({'top':sA},ms);
    $('.sN').css({color:'#598dbe'});
    $('#sN'+sC).css({color:'#464646'});
    $('#fD > div').fadeOut(ms,function(){
     $(this).css({display:'none'});
    });
    $('#fD-'+sC).fadeIn(ms,function(){
     $('#fD-'+sC).css({display:'block'});
    });
    console.log(toSlide);
}
function autoSlide(ms) {
    if(sC < 4) {
     sC++;
     sA = -(sC - 1) * pH;
    } else {
     sC = 1;
     sA = 0;
    }
    slide(ms);
    if(toSlide = 1) {
     setTimeout ( "autoSlide(sT)", 3000 );
    }
}
$(document).ready(function() {
    setTimeout ( "autoSlide(sT)", 3000 );
    $('#sL').click(function(){
     toSlide = 0;
     if(sC > 1) {
      sC--;
      sA = -(sC - 1) * pH;
     } else {
      sC = 4;
      sA = -3 * pH;
     }
     slide(sT);
     return false;
    });
    $('#sN').click(function(){
     toSlide = 0;
     if(sC < 4) {
      sC++;
      sA = -(sC - 1) * pH;
     } else {
      sC = 1;
      sA = 0;
     }
     slide(sT);
     return false;
    });
    $('.sN').click(function(){
     toSlide = 0;
     var sNid = this.id.split('sN');
     sC = sNid[1];
     sA = -(sC - 1) * pH;
     slide(sT);
     return false;
    });
});
+8  A: 
if (toSlide = 1) {

This is an assignment not a comparison. You want to do:

if (toSlide === 1) {

The altered full function:

function autoSlide(ms) {
    if(sC < 4) {
        sC++;
        sA = -(sC - 1) * pH;
    } else {
        sC = 1;
        sA = 0;
    }
    slide(ms);
    if(toSlide === 1) {
        setTimeout ( "autoSlide(sT)", 3000 );
    }
}
Garry Shutler
Ah, thank you! I'm new with Javascript, so I didn't realize the difference between the two.
Kerrick
+2  A: 

if(toSlide = 1) should be if(toSlide == 1)

This has bitten many people in c style languages

SQLMenace
A: 

These guys' answer is the solution. I don't know whether you already use it, but with Firebug it's quite easy to track down such bugs in the code.

Juri