Repeat with pause

Z

ZT.Ph34rl3ss

Hi there,

First off thank you for reading.

I'm trying to get the following to work.
I want a script that does the following 9 times: jah(urlOne); and then
1 time: jah(urlTwo); then it should start at the top again.
There has to be a break in between, thats why i have the wait function
there.
Hope anybody can help me because i'm not seeing my mistake and can't
think clear any more.

Thank you for reading.

<script language="javascript" type="text/javascript">


var urlOne="messageCenter.php, msgOne";
var urlTwo="messageBanner.php, msgOne";
var waitTime="1000";

alert (urlOne);

function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
}


alert (urlOne + " voor de statment");
jah(urlOne);
alert (urlOne + " na de statment");
wait(waitTime);

</script>
 
J

Joost Diepenmaat

Hi there,

First off thank you for reading.

I'm trying to get the following to work.
I want a script that does the following 9 times: jah(urlOne); and then
1 time: jah(urlTwo); then it should start at the top again.
There has to be a break in between, thats why i have the wait function
there.
Hope anybody can help me because i'm not seeing my mistake and can't
think clear any more.

You've not explained what's going wrong.

Anwyay:
function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
}

NEVER EVER DO THAT. It will use up 100% of your CPU and probably will
block your host (browser) completely until the timeout is
finished. Possibly the host will break your script to prevent that.

Use setTimeout() instead; something like:

var count = 0;
function nineTimes() {
if (count++ < 9) {
jah(urlOne);
setTimeout(nineTimes, 1000);
}
else {
setTimeout(function() { jah(urlTwo) },1000);
}
}
nineTimes();
 
V

VK

I want a script that does the following 9 times: jah(urlOne); and then
1 time: jah(urlTwo); then it should start at the top again.
There has to be a break in between, thats why i have the wait function
there.

Using "long loop" to emulate wait() method. That will not work for two
reasons:

1) as already pointed out by other respondent it takes all resources
so may effectively freeze the interface.
2) this is why most probably at some point it will trig the internal
security break and the user will see the popup box "A script on this
page takes too much time to execute, abort?" or similar message. I'm
sure you saw such messages while visiting some other sites - and now
your know the most often reason of it.

So we need window.setTimeout and the whole task is far of being
trivial. I am giving here a sample of how it could be done for a DOM
node. But as "urlOne" and "urlTwo" suggest you are planning to load
timed and looped external pages into (i)frame. This task is not
reliably solvable at all from the main page alone: because you cannot
predict server response time and the download speed. If you are
changing the pages every 1 sec but the actual time to get the page is
say 1.5 or 2 sec your show will end up by showing nothing at all. That
means that each involved page has to inform your script that it is
loaded, and your script respectively has to manage race conditions so
either switch the page on timer or on actual load depending on what
happened first.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en-US">
<head>
<meta http-equiv="Content-type"
content="text/html; charset=iso-8859-1">
<title>Demo</title>
<style type="text/css">
html * {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
</style>
<script type="text/javascript">

// relevant to your question

var URL = [
['URL 1',8,1000]
,['URL 2',1,3000]
];
URL.arrayPosition = 0;
URL.loopCount = 0;

function loop() {
SELECT_POSITION:
do {
if (URL.loopCount >= URL[URL.arrayPosition][1]) {
if (URL.arrayPosition < URL.length-1) {
++URL.arrayPosition;
}
else {
URL.arrayPosition = 0;
}
URL.loopCount = 0;
continue SELECT_POSITION;
}
else {
break SELECT_POSITION;
}
} while(true);
URL.loopCount++;
$('out').innerHTML = URL[URL.arrayPosition][0].italics().
concat(' display ',
URL.loopCount, ' out of ',
URL[URL.arrayPosition][1]);
URL.timerID = window.setTimeout('loop()',URL[URL.arrayPosition][2]);
}

// end of relevant to your question

function $(id) {
return document.getElementById(id);
}

function init() {
loop();
}
function paintAndInit() {
window.setTimeout('init()',10);
}
window.onload = paintAndInit;
</script>
</head>
<body>
<h1 id="out">Welcome</h1>
</body>
</html>
 
D

Dr J R Stockton

In comp.lang.javascript message <df3abeb1-2f79-4688-ad90-3ec3aefa3241@i2
9g2000prf.googlegroups.com>, Sat, 22 Mar 2008 07:49:49,
function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
}

Wait loops are bad practice; but bloated ones are worse. In any loop,
one should instinctively repeat as little as possible, for (in other
circumstances) speed.

function wait(ms) {
ms += +new Date()
while (new Date() < ms) {} }
 
Z

ZT.Ph34rl3ss

Thank you all 3 of you, i'm amazed by the answer and the speed the
were provided in.
Will post my code and the site when i'm done.

Thanks again!

Gr,

Jeroen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top