Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timer takes longer sometimes 1

Status
Not open for further replies.

aliashippysmom

Programmer
Jul 29, 2004
43
US
HI! I have a javascript function which executes when the page loads. It displays logos on my page. It re-executes using setTimeOut every 3 seconds and changes the logo. Seems to work fine except sometimes a logo will remain on the page what seems to be longer than 3 seconds perhaps 5-7 seconds. What could cause this delay and is there anything I can do about it? Is this normal and maybe has something to do with allocation of resources or something?

Thanks in advance for any replies.
 
Duh...I think I may have figured this out. Since the images are selected with the help of a random number generator, there is nothing to prevent the same image from being selected consecutively and therefore, since the image will not change, it appears that it's there for a longer amount of time. Here's the code anyway. I guess I have to add something so that the same random number is not selected consecutively. If anyone has any simple way to solve this, it would be much appreciated. Here's the code anyway:

Code:
<script language="javascript">
var d=document;
var timerID=null;
var galleypics=["fallicon2.gif","analog.gif","justdesserts.gif"];
var galleylinks=["[URL unfurl="true"]http://www.fallcomm.com/aboutus/OurMission.asp",[/URL]
"[URL unfurl="true"]http://www.google.com","http://www.google.com"[/URL]];
var preloads=new Array();
for(var i=0;i<galleypics.length;i++){
 preloads[i]=new Image();
 preloads[i].src="/images/"+galleypics[i];
}
function randPic(){
var picIndex=Math.floor(Math.random()* galleypics.length);

var picIndex2=picIndex+4;
var link1="";
if(picIndex2>=galleypics.length){
picIndex2-=falleypics.length;
}
d.images["pic1"].src="images/"+galleypics[picIndex];
link1 = galleylinks[picIndex];
d.links[11].href = link1;
timerID=setTimeout("randPic()",3000);
}
</script>

Here's the body tag:

Code:
<body LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0"  onLoad="randPic()
;MM_preloadImages('/images/Header_r3_c1_f2.gif',
'/images/Header_r3_c1_f3.gif','/images/Header_r3_c2_f2.gif',
'/images/Header_r3_c2_f3.gif','/images/Header_r3_c3_f2.gif',
'/images/Header_r3_c3_f3.gif','/images/Header_r3_c4_f2.gif',
'/images/Header_r3_c4_f3.gif','/images/Header_r3_c5_f2.gif',
'/images/Header_r3_c5_f3.gif','/images/Header_r3_c6_f2.gif',
'/images/Header_r3_c6_f3.gif','/images/Header_r3_c7_f2.gif',
'/images/Header_r3_c7_f3.gif','/images/Header_r3_c8_f2.gif',
'/images/Header_r3_c8_f3.gif')">

Notice randPic() is called first in the body tag before the other images are preloaded...

Any ideas? Thanks!
 
Changing this:

Code:
function randPic(){
var picIndex=Math.floor(Math.random()* galleypics.length);

to this:

Code:
var picIndex = -1;
function randPic(){
   var tempPixIndex = Math.floor(Math.random()* galleypics.length);
   while (tempPixIndex == picIndex) tempPixIndex = Math.floor(Math.random()* galleypics.length);
   picIndex = tempPixIndex;

should do what you want.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top