473,749 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why the delay please?

Amy
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changel ink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math .random()*arr.l ength);
document.getEle mentById("url") .href = arr[rand][1];
document.getEle mentById("url") .innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('ch angelink()', 1000);

Nov 7 '06 #1
17 1787
Lee
Amy said:
>
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changel ink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math .random()*arr.l ength);
document.getEl ementById("url" ).href = arr[rand][1];
document.getEl ementById("url" ).innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('c hangelink()', 1000);
Sometimes your computer is busy doing something else.
Can you explain the purpose of the "counter" variable?
This still looks like a homework assignment, to me.
--

Nov 7 '06 #2

Amy wrote:
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
Think about it - what happens if we are displaying the content of
arr[0] and we pick the random number 3 as the next one to display?
Would the script appear to pause?

Nov 7 '06 #3

n...@chthonic.f 9.co.uk wrote:
>
Think about it - what happens if we are displaying the content of
arr[0] and we pick the random number 3 as the next one to display?
Would the script appear to pause?
Scratch that - I meant if we pick the random number 0 as the next one
to display.

You can work out for yourself what to do next...

Nov 7 '06 #4
Lee
Amy said:
>
Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changel ink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math .random()*arr.l ength);
alert(rand);
>document.getEl ementById("url" ).href = arr[rand][1];
document.getEl ementById("url" ).innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('c hangelink()', 1000);
Even if this is an assignment, I suppose it doesn't hurt to
show you how to debug it. Add the alert() above and watch
how the values change. You'll see it more quickly if you
reduce the number of elements in arr while testing.
--

Nov 7 '06 #5
Amy
Thank you very much. This isn't homework at all, I provide all the
javascript almost completely done which is more than most people do.
You guys already seem to know what to do. I still don't know what your
trying to tell me. Can't seem to find that alert you told me to try.
Are you trying to tell me that with random arrays since its random it
takes longer sometimes to pick its next choice? Thats what causes a
delay? If thats the case why don't I have problem with other second
random arrays that I have tested? I'll slow down the timer and see what
happens and let you know. If there is something else I am messing
please tell me what it is.

Lee wrote:
Amy said:

Hi, I finished this script and for some reason there is a delay
every so often in the timing. Sometimes
it seems two take 2 seconds instead of 1.
Can anyone see anything that would slow it down? I sure can't see it.
Thank you very much, Amy <body onload="changel ink()"><a href="#"
id="url"></a>

arr = new Array();
arr[0] = ["Link 1","http://www.google.com"];
arr[1] = ["Link 2","http://www.msn.com"];
arr[2] = ["Link 3","http://www.yahoo.com"];
arr[3] = ["Link 4","http://www.google.com"];
arr[4] = ["Link 5","http://www.msn.com"];
arr[5] = ["Link 6","http://www.yahoo.com"];
arr[6] = ["Link 7","http://www.msn.com"];
arr[7] = ["Link 8","http://www.google.com"];
arr[8] = ["Link 9","http://www.yahoo.com"];
arr[9] = ["Link 10","http://www.msn.com"];
arr[10] = ["Link 11","http://www.yahoo.com"];
arr[11] = ["Link 12","http://www.google.com"];
arr[12] = ["Link 13","http://www.msn.com"];
arr[13] = ["Link 14","http://www.yahoo.com"];
arr[14] = ["Link 15","http://www.google.com"];
counter = 0;

function changelink()
{
rand = Math.floor(Math .random()*arr.l ength);
alert(rand);
document.getEle mentById("url") .href = arr[rand][1];
document.getEle mentById("url") .innerHTML = arr[rand][0];
counter++;

if (counter == arr.length)
counter = 0;
}

setInterval('ch angelink()', 1000);

Even if this is an assignment, I suppose it doesn't hurt to
show you how to debug it. Add the alert() above and watch
how the values change. You'll see it more quickly if you
reduce the number of elements in arr while testing.
--
Nov 7 '06 #6
Amy
Are you trying to tell me I don't need the counter variable since its
random?

Making it a longer timer didn't help, it doubles the timing on some
items no matter what.

Nov 7 '06 #7
Amy
Don't know if thats what you are trying to tell me or not but makes
perfect sense so I took all the counters out. To this.

function changelink()
{
var rand = Math.floor(Math .random()*conte nt.length);
document.getEle mentById("url") .href = content[rand][1];
document.getEle mentById("url") .innerHTML = content[rand][0];
}

setInterval("ch angelink()", 2000);

Same problem though, still a delay. I sure hope someone can help.

Nov 7 '06 #8
Lee
Amy said:
>
Thank you very much. This isn't homework at all, I provide all the
javascript almost completely done which is more than most people do.
You guys already seem to know what to do. I still don't know what your
trying to tell me. Can't seem to find that alert you told me to try.
Are you trying to tell me that with random arrays since its random it
takes longer sometimes to pick its next choice? Thats what causes a
delay? If thats the case why don't I have problem with other second
random arrays that I have tested? I'll slow down the timer and see what
happens and let you know. If there is something else I am messing
please tell me what it is.
If the same random number is generated twice in a row, the
link won't change, and so it will appear to be taking longer.
The following includes simple code to ensure that there are
no repeats by incrementing the value if necessary. You could
just create random numbers until you find one that's not the
same as the previous one, but this way has the advantage that
it even works if arr.length=1. We sacrifice some randomness
for that, but it's a good bargain in this case. Another way
to go would be to generate a shuffled list of indices and
iterating through it, so you never see a repeat until you've
seen all of the links, but that's more complicated.
You'll find more than you probably want to know at:
http://www.merlyn.demon.co.uk/js-randm.htm
var previous=-1;
function changelink() {
var rand = Math.floor(Math .random()*arr.l ength);
if ( rand == previous ) {
rand++;
if ( rand == arr.length ) {
rand=0;
}
}
previous=rand;
document.getEle mentById("url") .href = arr[rand][1];
document.getEle mentById("url") .innerHTML = arr[rand][0];
}
--

Nov 7 '06 #9
Amy wrote on 07 nov 2006 in comp.lang.javas cript:
Don't know if thats what you are trying to tell me or not but makes
perfect sense so I took all the counters out. To this.
What are you talking about, Amy?

Could you please adhere to usenet netiquette and quote what you are
reacting on, as this is not email but usenet posting.

[and when you quote, please do not toppost]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
18832
by: Des L. Davis | last post by:
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003 Server Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000 Server If you run the code below, you'll notice something odd occuring. The MilliSecond value does not change after a 1Millisecond delay. Is this a bug or am I doing something wrong? Any assistance will be greatly appreciated
1
5272
by: Troy | last post by:
Hello- I have a need to allow a user to reload a page on a chosen interval (selected from a select box) or to chose not to reload the page at all (default). I would like to do this in javascript. Currently I am using the meta tag as default, no refresh:
1
1419
by: John Smith | last post by:
Hello! I have following developing environment: Visual Studio.NET 2003 ..NET Framework 1.1 local ASPNET account exists Windows XP Professional All worked perfectly for months.
6
7245
by: adish11 | last post by:
Hi, In Web Application I would like to create delay for 30 sec in the code to enable external process to run. During the delay in application I would like to show the user message like "Please wait while the data being updated..." Can u help me please with code lines? Thank u very much for the help and the answer.
11
21254
by: Maheshkumar.R | last post by:
Hi groups, How i can introduce some milliseconds delay in application. How i can do achieve this... let me clearly say... (1) I'm displaying slices of medical images. For framerate - for this i'm loading 2+ slices, 4+ slices in loop to have cine impact..-> Ok working (2) But if i want to reduce the speed...how i can introduce delay inbetween
4
782
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I make a 10 second delay? ----------------------------------------------------------------------- There is no built-in way to pause execution in javascript such as a sleep function, but hosts usually provide a method of some form. Web browsers are designed for event driven programming and only provide the « setTimeout » and « setInterval »...
1
7006
by: maul581 | last post by:
I know the delay( ) function of Turbo C and used it in my project. By this function we can give intermediate delay in execution from 1 millisecond to 9999 millisecond. My main question is "Is it possible to get delay less than 1 millisecond in Turbo C"? If by changin any header file (e.g. dos.h) or by creating my own "delay" function, at any way Can I get delay less than 1 millisecond? If in C it is not possible than please suggest me any...
1
2970
by: maul581 | last post by:
I know the delay( ) function of Turbo C and used it in my project. By this function we can give intermediate delay in execution from 1 millisecond to 9999 millisecond. My main question is "Is it possible to get delay less than 1 millisecond in Turbo C"? If by changin any header file (e.g. dos.h) or by creating my own "delay" function, at any way Can I get delay less than 1 millisecond? If in C it is not possible than please suggest me any...
0
1811
by: stirucherai | last post by:
Here is my what I am trying to do 1. Open a MP3 file (Sound) -- Multiple songs -- roughly 30 seconds per song 2. Dynamically create TEXT Field using CreateText method 3. Show Lyrics for the First song and start MP3 4. When MP3 moves to second song (I trap it based on time) -- I stop the Mp3 using StopALLSounds. 5. Change the Text to new Lyric (second song lyrics) 6. introduce delay -- using multple methods -- like simple while loop for...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.