...

View Full Version : prevent immediate repeat?



resin
06-24-2012, 06:55 PM
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?



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?

DrDOS
06-24-2012, 07:31 PM
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..

Philip M
06-24-2012, 08:04 PM
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.

resin
06-25-2012, 05:39 AM
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??

Philip M
06-25-2012, 09:15 AM
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):-


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

resin
06-25-2012, 12:30 PM
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.

Philip M
06-25-2012, 01:34 PM
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.

resin
06-25-2012, 03:09 PM
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 :(

Philip M
06-25-2012, 04:54 PM
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! :o 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.

resin
06-26-2012, 11:57 AM
Ah I should have caught that. Gratias! It tis working now, and really is wonderful!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum