Javascript. Cycle misconduct



  • After the call of TurnOFFALL, on the second irration of the cycle, the TurnON code (activated in response to the TurnOFFALL click) in the branch containing console.log(dev+"+checkbox); the second iteration is unclear. For volumes 1 and 3, it works.

    Суть проблемы

     <div class="lightblock">
                    <center><div class="llabel"><h3>text</h3></div></center>
                    <a style="opacity:0.9;" name="fourpins-3" id="fourpins" data-on="0" onclick='TurnChanell(this);'>1</a>
                    <a id="fourpins" name="fourpins-3" data-on="0" onclick='TurnChanell(this);'>2</a>
                    <a style=" clear: left;" name="fourpins-3" id="fourpins" data-on="0" onclick='TurnChanell(this);'>3</a>
                    <a style="opacity:0.9;" name="fourpins-3" id="fourpins" data-on="0" onclick='TurnChanell(this);'>4</a>
                    <a href='#' onclick='TurnON(this);' style="float:left;height:40px;width:80px;margin-left:-140px;margin-top:-20px;z-index: 9999;">
                        <div class="switch">
                        <input id="cmn-toggle-3" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-3" id="lab3">0</label>
                    </div>
                    </a>                
                </div>
    

    function TurnON(checkbox){
    if(count!=0){ count=0;}
    else{
    var dev = checkbox.getElementsByTagName("LABEL")[0].htmlFor.replace("cmn-toggle-", "");
    if(checkbox.getElementsByTagName("LABEL")[0].innerText == '1'){
    console.log(dev+" "+checkbox);
    checkbox.getElementsByTagName("LABEL")[0].innerText = 0;
    var el = document.getElementsByName("fourpins-"+dev);
    for (var i = 0; i < el.length; i++) {
    if(el[i].getAttribute("data-on") == "1")
    el[i].click();
    }
    }else{
    checkbox.getElementsByTagName("LABEL")[0].innerText = 1;
    var el = document.getElementsByName("fourpins-"+dev);
    for (var i = 0; i < el.length; i++) {
    if(el[i].getAttribute("data-on") == "0")
    el[i].click();
    }
    }
    count++;
    }
    }

        function TurnOFFALL(){
            for(var i = 1;i &lt;= 3;i++) {               
                if( document.getElementById('lab'+i).innerText == "1"){                   
                    document.getElementById('cmn-toggle-'+i).click();
                }
            }
        }
    



  • Looks like the problem is:

    if(count!=0){ count=0;}
    else{ 
    

    In your first iteration, your count is 0 and your block is being executed by adding count. In the second iteration, count is not equal to 0 and a block that makes count equal 0 and does not fulfil your code. Since counts have been equal 0, the third iteration is performed.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2