I have some really bizarre behavior...and I'm wondering if its because I
don't fully understand setTimeout(). I have a web page that calls a
function on the Onload. This function calls two separate functions and then
uses setTimeout() to keep calling itself. Each function randomly generates
a number and then I update the image.src with that.
OK...it works...but here's the thing: If I run setTimeout() on just one
function by itself, it almost always displays a new pic...but when I put the
setTimeout() in the startup function a get a lot of duplicates. I've been
watching it for a long time and I don't think it's a coincidence. Any idea
as to why it seems like both functions don't run/update the pic all the
time? If I uncomment the line, alert("hello"), it runs all the time? Hmm...
Here's my code:
Index.shtml:
blah blah
<script src="attachments/jsfunctions.js" type="text/javascript"></script>
</head>
<body onLoad="preLoad(); StartPic(); GetSpecialPic();">
blah blah
jsfunctions.js:
function GetPic()
{
var picversions = 7; // number of versions of pic
var lnow = new Date();
var ltime = lnow.getTime();
var lrand = ((ltime % 4652353) * 492113) + 1;
var lImg = "images/technically" + (lrand % picversions) + ".jpg";
document.images["imgMenu"].src = lImg;
window.defaultStatus = lImg;
}
function GetMainPic()
{
//alert("hello");
var mainpicversions = 2; // number of versions of pic
var lmainnow = new Date();
var lmainday = lmainnow.getDay() + 1;
var lmainpic = (lmainday % mainpicversions);
var subpicversions = 4; // number of versions of subpic
var lnow = new Date();
var ltime = lnow.getTime();
var lrand = ((ltime % 4652353) * 492113) + 1;
var lsubpic = (lrand % subpicversions);
var lImg = "images/mainpic" + lmainpic + "_" + lsubpic + ".jpg";
document.images["imgMainpic"].src = lImg;
}
function StartPic()
{
GetPic();
GetMainPic();
setTimeout("StartPic();",3000);
}
TIA
-bruce duncan