473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

setTimeout()??

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
3 1448
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jupiter49 | last post by:
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
6
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id =...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
6
by: Max | last post by:
Hi, I'm not exactly sure why this doesn't work. I'm basically just trying a simple approach at a slide down div. function slide_div { ...
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
7
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.