2013-10-15 171 views
0

我有一个全局变量“isOnSecond”,它最初被设置为falseJavaScript逻辑问题

这是连接到检测布尔值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; 
} 
+0

首先,我会建议将所有if语句包装在'{}'中。但你总是在最后设置'isOnSecond'为'true',所以这可能是问题 –

+1

谢谢你们,得到它的工作,学校的男孩错误:) – user2696787

+0

你需要_deperly_ indent你的代码。问题应该是显而易见的。 – SLaks

回答

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

这将始终设置isOnSecond为真,即使你刚刚是正确的之前设置为false。相反,使用

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

if (isOnSecond) { 
    // do work only every second time 
} 
isOnSecond = !isOnSecond; 
0

您需要'else'结尾,否则isOnSecond将始终设置为true。

if (isOnSecond == true){ 
    //unhighlight all controls 
    //lots of code here 
    isOnSecond = false; 
} else { 
    isOnSecond = true; 
} 
+0

或者在第二个值被赋值之前退出该功能。 – Todd

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

isOnSecond = true; 

此代码将确保isOnSecond仅仅是一个永远假,只要它需要解释if语句来完成,并移动到下一个线,我可以向你保证是一个非常短的时间。

看来你应该在函数结尾处松开isOnSecond = true,并且只有当你真的需要它是真实的而不是总是时,才再次声明它是真的。

0

它看起来像你通过if语句

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

,然后设置isOnSecond为true

isOnSecond = true; 

运行,如果你这样做:

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

这将防止isOnSecond始终以“真实”出现。换句话说,如果它被设置为true,它就会变成假,如果它被设置为false,它就会变成真的。