rotate varaible/fields with javascript

Discussion in 'JavaScript' started by taylorswan, Aug 14, 2012.

  1. #1
    hi
    i am trying to rotate a field inside variable in javascript
    this is a part of a code:


    var a = { sr: 22, id: 31, jab:45124, domain: 'aaa.com' };


    i am trying to rotate the domain field :
    aaa.com with bbb.com and/or ccc.com
    i already tried rotating it in many ways including all the rotate methods for images banners etc but no luck so far.
    how can i do it?

    -also tried rotating the whole line "var a ....." but i couldn't do it too

    thanks
     
    taylorswan, Aug 14, 2012 IP
  2. veganin

    veganin Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Have a look at Math.random()...
     
    veganin, Aug 14, 2012 IP
  3. FilmFiddler

    FilmFiddler Greenhorn

    Messages:
    54
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    23
    #3
    Perhaps something like this:

    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    var a = {
      "sr": 22,
      "id": 31,
      "jab": 45124,
      "domain": [
        "aaa.com",
        "bbb.com",
        "ccc.com"
      ]
    };
    
    var which = 0,
        show,
        len;
    
    function rotateDomain() {
      show.innerHTML = a["domain"][which];
    
      which = ++which % len;
      // which = Math.floor(Math.random()*len); // or randomized alternative
    
      setTimeout(rotateDomain, 3000);
    }
    
    window.onload = function() {
      show = document.getElementById("showit");
      len = a["domain"].length;
      rotateDomain();
    }
    </script>
    </head>
    <body>
    <div>
      <a href="#" id="showit"></a>
    </div>
    </body>
    </html>
    Code (markup):
     
    FilmFiddler, Aug 14, 2012 IP