简体   繁体   English

为什么我的变量会不断重置自身?

[英]Why does my variable keep resetting itself?

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). 我有一个为网站构建的图像滑块,并且将其设置为每3秒滑动一次(诊断后速度会变慢,但我不希望等待10秒才能看到问题所在)。 I also have it set so that before it auto-slides, it checks if the toSlide variable is set to 1 (default) or not. 我还设置了它,以便在它自动滑动之前,它检查toSlide变量是否设置为1(默认值)。 When the user clicks on a next/previous link or to a certain slide, it sets toSlide to 0. 当用户点击下一个/前一个链接或在一定滑动,它设置toSlide为0。

My problem is that it auto-slides again, then sets toSlide back to 1, and I can't figure out why. 我的问题是它会再次自动滑动,然后将toSlide设置回1,我不知道为什么。 Could anybody help me please? 有人可以帮我吗?

You can see the HTML on my site , and here is the Javascript: 您可以在我的网站上看到HTML,这是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;
    });
});
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 );
    }
}

if(toSlide = 1) should be if(toSlide == 1) if(toSlide = 1)应该是if(toSlide == 1)

This has bitten many people in c style languages 这已经用c风格的语言咬了很多人

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. 我不知道您是否已经使用过它,但是使用Firebug ,在代码中查找此类错误非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM