By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,045 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,045 IT Pros & Developers. It's quick & easy.

setTimeout()??

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Lee
Bruce D said:

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.
var lnow = new Date();
var ltime = lnow.getTime();
var lrand = ((ltime % 4652353) * 492113) + 1;
var lImg = "images/technically" + (lrand % picversions) + ".jpg";

That's a good reason to avoid using the date to generate random numbers.
If you call both functions onload, it.s unlikely that the two times will be
different. Date doesn't really track time to the millisecond. That's just the
unit that it uses for storage.

Javascript provides a better generator, anyway:

var lImg = "images/technically" + Math.floor(Math.random()*picversions) +".jpg";

Jul 23 '05 #2

P: n/a
"Lee" <RE**************@cox.net> wrote in message
news:cr*********@drn.newsguy.com...

That's a good reason to avoid using the date to generate random numbers.
If you call both functions onload, it.s unlikely that the two times will be different. Date doesn't really track time to the millisecond. That's just the unit that it uses for storage.

Javascript provides a better generator, anyway:

var lImg = "images/technically" + Math.floor(Math.random()*picversions)

+".jpg";

Lee,
Thanks for the new random generator...I'm going to test it out.
But I think you misunderstood my problem. I don't care if it radomly pics
the same number because if you notice the functions they update different
pics. The problem I was having is that it wasn't updating the pics...like
it wasn't running the code.
Well...as it turns out...it's fixed now...and I didn't do a damn thing?!? I
viewed my web page on another machine and notice it was loading new pics
each time. I rebooted and BAM...it's working fine. Wierd thing...if I make
changes to the script...sometimes...it will stop updating the pics....but
only on my PC. I'll blam CACHE on this one...or a small gremlin!
Anyhow...the code actually works.

cheers to all!
-bd
Jul 23 '05 #3

P: n/a
"Lee" <RE**************@cox.net> wrote in message
news:cr*********@drn.newsguy.com...
That's a good reason to avoid using the date to generate random numbers.
If you call both functions onload, it.s unlikely that the two times will be different. Date doesn't really track time to the millisecond. That's just the unit that it uses for storage.

Javascript provides a better generator, anyway:

var lImg = "images/technically" + Math.floor(Math.random()*picversions)

+".jpg";
Aren't there some versions of JS that don't support Math.random()? Should I
worry about it?

-bruce
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.