为什么我的变量会自行重置?

时间:2022-05-24 16:46:13

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.

我有一个我为我的网站构建的图像滑块,我设置为每3秒滑动一次(诊断后会慢一点,但我不想等待10秒钟才能看到错误)。我也将它设置为在自动滑动之前,它会检查toSlide变量是否设置为1(默认值)。当用户单击下一个/上一个链接或某个幻灯片时,它会将滑动设置为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?

我的问题是它再次自动滑动,然后设置为滑回到1,我无法弄清楚原因。请问有人帮帮我吗?

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;
    });
});

3 个解决方案

#1


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 );
    }
}

#2


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

if(toSlide = 1)应为if(toSlide == 1)

This has bitten many people in c style languages

这已经咬了许多c风格的人

#3


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可以很容易地在代码中找到这些错误。

#1


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 );
    }
}

#2


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

if(toSlide = 1)应为if(toSlide == 1)

This has bitten many people in c style languages

这已经咬了许多c风格的人

#3


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可以很容易地在代码中找到这些错误。