简体   繁体   中英

Javascript logic Issue

I have a global variable "isOnSecond" which is initially set to false

This is connected to an if statement that detects the boolean value, if it is true some actions are performed and then the boolean is set back to false. therefore I want the action to be performed half the time.

My questions is this, I know I have logic problems in this function however I cant seem to conjure up a solution at this point.

How should I re work this function with my desired logic?

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

You need an 'else' at the end, or isOnSecond will always be set to true.

if (isOnSecond == true){
    //unhighlight all controls
    //lots of code here
    isOnSecond = false;
} else {
    isOnSecond = true;  
}
 if (isOnSecond == true) { // do work only every second time isOnSecond = false; } isOnSecond = true; 

That will always set isOnSecond to true, even if you had just set it to false right before. Instead, use

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

or

if (isOnSecond) {
    // do work only every second time
}
isOnSecond = !isOnSecond;
if ($this === $that) {
    //Logic
    isOnSecond = false;
}

isOnSecond = true;

This code will ensure that isOnSecond is only ever false for a as long as it takes the interpreter to finish the if statement and move to the next line, which I can assure you is a very short period of time.

It seems that you should loose the isOnSecond = true at the end of the function and only declare it true again when you actually need it to be true rather than always.

It looks like you run through the if statement

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

and then set isOnSecond to true

isOnSecond = true;

if you do this:

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

This will prevent isOnSecond always coming out as 'true'. In other words, if it's set to true, it'll become false and if it's set to false, it'll become true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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