Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts

    prevent immediate repeat?

    Good day all

    I have this script that stores a set of divs and displays them in a random order. Works great, but I'm trying to find a way to prevent the same div from being displayed twice in a row, without using cookies?

    Code:
    NumberOfDivsToRandomDisplay = 15;
    var CookieName = 'DivRamdomValueCookie';
    function DisplayRandomDiv() {
    var r = Math.ceil(Math.random() * NumberOfDivsToRandomDisplay);
    if(NumberOfDivsToRandomDisplay > 1) {
       var ck = 0;
       var cookiebegin = document.cookie.indexOf(CookieName + "=");
       if(cookiebegin > -1) {
          cookiebegin += 1 + CookieName.length;
          cookieend = document.cookie.indexOf(";",cookiebegin);
          if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
          ck = parseInt(document.cookie.substring(cookiebegin,cookieend));
          }
       while(r == ck) { r = Math.ceil(Math.random() * NumberOfDivsToRandomDisplay); }
       //document.cookie = CookieName + "=" + r;
       }
    for( var i=1; i<=NumberOfDivsToRandomDisplay; i++) {
       document.getElementById("randomDiv"+i).style.display="none";
       }
    document.getElementById("randomDiv"+r).style.display="block";
    }
    DisplayRandomDiv()
    setInterval('DisplayRandomDiv()', 35000)
    As you can see there is some cookie script in there but I'm trying to do this without using cookies, which I believe is possible as I've seen similar scripts do it without cookies. So if this is indeed possible, how would I do it?

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    You could maybe store the information in the little known and seldom used window.name object instead of cookies, assuming window.name isn't already being used..

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    var previous = "";
    for( var i=1; i<=NumberOfDivsToRandomDisplay; i++) {
    document.getElementById("randomDiv"+i).style.display="none";
    }
    var r = Math.ceil(Math.random() * NumberOfDivsToRandomDisplay);
    if (r!= previous) {
    document.getElementById("randomDiv"+r).style.display="block";
    previous = r;
    }
    else {
    DisplayRandomDiv();  // try again
    }

    Expecting the world to treat you fairly because you are good is like expecting the bull not to charge because you are a vegetarian.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Philip, this isn't working for me. I tried running your code as is, and tried mixing in some of the old code but can't get it to work. Also I want to set a setTimeout on this, so that the first div doesn't appear (nor does the rotation cycle begin) until 30 seconds. And I still want to implement the setInterval of 35 secs as well. So basically, the first div should appear at 30 seconds after the page loads, then another div replaces the first div 35 secs after that, then another 35 secs after that, and so on. Would you mind explaining exactly how to accomplish this??

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by resin View Post
    Philip, this isn't working for me. I tried running your code as is, and tried mixing in some of the old code but can't get it to work. Also I want to set a setTimeout on this, so that the first div doesn't appear (nor does the rotation cycle begin) until 30 seconds. And I still want to implement the setInterval of 35 secs as well. So basically, the first div should appear at 30 seconds after the page loads, then another div replaces the first div 35 secs after that, then another 35 secs after that, and so on. Would you mind explaining exactly how to accomplish this??
    You asked for "I'm trying to find a way to prevent the same div from being displayed twice in a row". The code I gave you in Post#3 does that. A better way is to shuffle your list of divs so that all are shown in random order but they cannot repeat at all. Here you are (adjust the number of divs as required):-

    Code:
    <html> 
    <head> 
    <body>
    
    <div id = "randomDiv0" style="display:none">0</div>
    <div id = "randomDiv1" style="display:none">1</div>
    <div id = "randomDiv2" style="display:none">2</div>
    <div id = "randomDiv3" style="display:none">3</div>
    <div id = "randomDiv4" style="display:none">4</div>
    
    <script type="text/javascript">
    
    window.onload = function() {
    setTimeout (showTheDivs, 30000);  // 30 seconds
    }
    
    var myArray = [];
    var numberOfDivs = 5;  // change to suit
    for (var i = 0; i <numberOfDivs; i++) {  // set up the array
    myArray[i] = i;
    }
    
    function shuffle(Arr) {
    var NewArr = Arr.slice(0);  // copy the array
    var len = NewArr.length;
    while (len>0) {
    NewArr.push(NewArr.splice(Math.floor(Math.random()*len),1)[0]);
    len--;
    }
    return NewArr;
    }
    
    var shuffled = shuffle(myArray);  // shuffle the array
    var count = 0;
    
    function showTheDivs() {
    
    for (var i=0; i<numberOfDivs; i++) {
    document.getElementById("randomDiv"+i).style.display="none";  // hide them all
    }
    
    var index = shuffled[count];
    document.getElementById("randomDiv"+index).style.display="block";  // show the next random div
    count ++;
    if (count > (numberOfDivs-1)) {
    var shuffled = shuffle(myArray);  // shuffle the array again or delete this line to go round again in the same random order
    count = 0; // and reset the counter
    }
    
    setTimeout(showTheDivs,35000);  // repeat after 35 seconds
    
    }
    
    </script>
    
    
    
    </script>
    </body>
    </html>
    Last edited by Philip M; 06-25-2012 at 08:51 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts
    You're right, I thought I'd be able to figure out how to intergrate it into my original code but unfortunately I didn't. Anyway as far as the last code you posted, it's not working for me. Sorry for my ignorance, but is there something else here I need to modify besides the number of divs? Here is the test page- http://resin.webs.com/new4.html . I'm not seeing anything show up.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by resin View Post
    You're right, I thought I'd be able to figure out how to intergrate it into my original code but unfortunately I didn't. Anyway as far as the last code you posted, it's not working for me..
    It works just fine for me The fault, dear Brutus, lies not in the stars ....

    I guess there is a conflict with one of your adverts. Free hosts are often not worth the money you pay to use them.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Arr I suspected that. My web host has been having issues lately. Want to get new host, but haven't found another free host that lets you upload your own html anymore.. Here's another one for the stars- http://resin.webs.com/new.html , not sure if you already saw that one or not. But yea I'm getting fed up with them. May have to switch to pay host sooner than I planned..

    Well I trust that it works like you say, so even though I currently can only imagine how wonderful this script is, I want to thank you anyway

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by resin View Post
    Hmm I tried running this locally but its still not even working for me that way. So this should run properly as is without changing anything? I'm not seeing the numbers pop up. Not seeing anything happen at all..
    So sorry! Mea culpa! I added a line at the last minute in the post which has an error.

    Delete the var keyword in

    var shuffled = shuffle(myArray); // shuffle the array again or delete this line to go round again in the same random order

    You may find it works on your site now.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ah I should have caught that. Gratias! It tis working now, and really is wonderful!


  •  

    Posting Permissions

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