Results 1 to 2 of 2

Thread: logic help, else if counter

  1. #1
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default logic help, else if counter

    I'm such a newb sometimes it's really frustrating.

    I've written scripts in autohotkey and other languages where this worked perfectly but for some reason in JS I'm shooting blanks.

    Here's the basic structure for the function I'm trying to make:
    Code:
    var count;
    function counter() {
    if(count = 0) {
    domycommand();
    count = 1;
    }
    else if(count = 1) {
    domycommand2();
    count = 2;
    }
    ...
    ...
    ...
    else if(count = n) {
    domycommand3();
    count = 0;
    }
    else {alert"count didn't work"};
    }
    Then in my main html I have a section for calling functions with intervals like:
    Code:
    <script type="text/javascript">
    onload = function() {
    setInterval('counter()',10000);
    When I load the page it does the first command and stops at the second state after doing domycommand2().

    What am I doing wrong? I need something to cycle and when it gets to the end it restarts back to 0. Simplicity being the primary factor................

    I have several uses I want this functionality for....in general.

    One is here:
    Code:
    function rotateDivs2() {
    var i;
     if(i =0){
      document.getElementById('div1').style.display = '';
      document.getElementById('div2').style.display = 'none';
      document.getElementById('div3').style.display = 'none';
     i =1;
     }
    else if (i =1) {
      document.getElementById('div1').style.display = 'none';
      document.getElementById('div2').style.display = '';
      document.getElementById('div3').style.display = 'none';
    i = 2;
     }
    else if(i =2) {
      document.getElementById('div1').style.display = 'none';
      document.getElementById('div2').style.display = 'none';
      document.getElementById('div3').style.display = '';
    i=0;
    }
    
    };
    Then in the html:
    Code:
    <script type="text/javascript">
    onload = function(){
     setInterval(rotateDivs2, 7000);
     setInterval("shuffle( $('#shuffle div') )", 7000);
     setInterval(ddshuffle, 7000);
    };
    
    
    <div id="div1">
    <p>some txt1 & image </p>
    </div>
    <div id="div2" style="display: none;">
    <p>some txt2 & image </p> 
    </div>
    <div id="div3" style="display: none;">
    <p>some txt3 & image </p> </div>
    </div>
    </div>
    When the page loads it displays the text in "div1" for the right duration, then switches to text in "div2" but never progresses to div3 and then back to the beginning.....

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    You've already got more code than you need. But when you're just starting out, that's not necessarily a bad thing.

    To answer your question though (I think, not 100% sure what the question is):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    var i = 0;
    function rotateDivs2() {
     if(i === 0){
      document.getElementById('div1').style.display = '';
      document.getElementById('div2').style.display = 'none';
      document.getElementById('div3').style.display = 'none';
     i = 1;
     }
    else if (i === 1) {
      document.getElementById('div1').style.display = 'none';
      document.getElementById('div2').style.display = '';
      document.getElementById('div3').style.display = 'none';
    i = 2;
     }
    else if(i === 2) {
      document.getElementById('div1').style.display = 'none';
      document.getElementById('div2').style.display = 'none';
      document.getElementById('div3').style.display = '';
    i = 0;
    }
    
    }
    onload = function(){
     rotateDivs2();
     setInterval(rotateDivs2, 7000);
    };
    </script>
    </head>
    <body>
    <div id="div1">
    <p>some txt1 &amp; image </p>
    </div>
    <div id="div2" style="display: none;">
    <p>some txt2 &amp; image </p> 
    </div>
    <div id="div3" style="display: none;">
    <p>some txt3 &amp; image </p> </div>
    </body>
    </html>
    With less code:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    var i = 0;
    function rotateDivs2() {
    	document.getElementById('div1').style.display =
    	document.getElementById('div2').style.display =
    	document.getElementById('div3').style.display = 'none';
    	document.getElementById('div' + (i++ + 1)).style.display = '';
    	if(i > 2){i = 0;}
    }
    onload = function(){
    	rotateDivs2();
    	setInterval(rotateDivs2, 7000);
    };
    </script>
    </head>
    <body>
    <div id="div1">
    <p>some txt1 &amp; image </p>
    </div>
    <div id="div2" style="display: none;">
    <p>some txt2 &amp; image </p> 
    </div>
    <div id="div3" style="display: none;">
    <p>some txt3 &amp; image </p> </div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    lmbarns (12-03-2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •