0

我有一个全局变量“isOnSecond”,最初设置为 false

这与检测布尔值的 if 语句相连,如果为真,则执行某些操作,然后将布尔值设置回 false。因此,我希望该动作执行一半。

我的问题是这样的,我知道我在这个函数中有逻辑问题,但是我现在似乎无法想出一个解决方案。

我应该如何用我想要的逻辑重新工作这个功能?

function transComplete()
{
    slideTransStep = 0;
    crtSlideIndex = nextSlideIndex;
    alert(isOnSecond);

    // for IE filters, removing filters re-enables cleartype
    if (nextSlide.style.removeAttribute)
        nextSlide.style.removeAttribute("filter");

    // show next slide
    showSlide((crtSlideIndex >= totalSlides) ? 1 : crtSlideIndex + 1);

    if (isOnSecond == true){
    //unhighlight all controls
    for (var i=0; i < slidesControllersCollection.length; i++){
        if (slidesControllersCollection[i].className === slideHighlightClass)
        slidesControllersCollection[i].className = ""; }

    // highlight the control for the next slide
    if (slidesControllersCollection[i].className === slideHighlightClass)       
    document.getElementById("slide-control-" + crtSlideIndex+1).className = slideHighlightClass;

    isOnSecond = false;
    }
    isOnSecond = true;  
}
4

4 回答 4

0
if ($this === $that) {
    //Logic
    isOnSecond = false;
}

isOnSecond = true;

isOnSecond只要解释器​​完成 if 语句并移至下一行,这段代码将确保它永远是错误的,我可以向您保证这是一个非常短的时间段。

似乎您应该松开isOnSecond = true函数末尾的 ,并且仅在您实际需要它为真时才再次将其声明为真,而不是总是如此。

于 2013-10-15T14:57:04.470 回答
0

看起来你运行了 if 语句

if (isOnSecond == true){
     stuff . . .
     isOnSecond = false;
}

然后将 isOnSecond 设置为 true

isOnSecond = true;

如果你这样做:

if (isOnSecond == true){
     stuff . . .
     isOnSecond = false;
}else{
    isOnSecond = true;
}

这将防止 isOnSecond 始终显示为“真”。换句话说,如果它设置为true,它将变为false,如果它设置为false,它将变为true。

于 2013-10-15T14:57:27.600 回答
0

最后需要一个“else”,否则 isOnSecond 将始终设置为 true。

if (isOnSecond == true){
    //unhighlight all controls
    //lots of code here
    isOnSecond = false;
} else {
    isOnSecond = true;  
}
于 2013-10-15T14:54:57.410 回答
0
if (isOnSecond == true) {
    // do work only every second time
    isOnSecond = false;
}
isOnSecond = true;

这将始终设置isOnSecond为 true,即使您之前刚刚将其设置为 false。相反,使用

if (isOnSecond) {
    // do work only every second time
    isOnSecond = false;
} else {
    isOnSecond = true;
}

或者

if (isOnSecond) {
    // do work only every second time
}
isOnSecond = !isOnSecond;
于 2013-10-15T14:55:11.670 回答