Repeat with pause

Discussion in 'Javascript' started by ZT.Ph34rl3ss@gmail.com, Mar 22, 2008.

  1. Guest

    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>
     
    , Mar 22, 2008
    #1
    1. Advertising

  2. "" <> writes:

    > 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();


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Mar 22, 2008
    #2
    1. Advertising

  3. VK Guest

    On Mar 22, 5:49 pm, "" <>
    wrote:
    > 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>
     
    VK, Mar 22, 2008
    #3
  4. In comp.lang.javascript message <df3abeb1-2f79-4688-ad90-3ec3aefa3241@i2
    9g2000prf.googlegroups.com>, Sat, 22 Mar 2008 07:49:49,
    "" <> posted:
    >
    >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) {} }

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
    <URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
    <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
     
    Dr J R Stockton, Mar 22, 2008
    #4
  5. Guest

    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
     
    , Mar 23, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Shohet
    Replies:
    10
    Views:
    1,944
    Kevin Spencer
    Jun 18, 2004
  2. GrantMagic

    pause a loop

    GrantMagic, Jul 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,993
  3. Leah

    Pause a web page?

    Leah, Mar 15, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,194
    Steve C. Orr [MVP, MCSD]
    Mar 15, 2006
  4. Rick
    Replies:
    4
    Views:
    41,691
    Roedy Green
    Oct 8, 2003
  5. E Tepp

    Pause Util Value is returned

    E Tepp, Dec 15, 2003, in forum: Java
    Replies:
    2
    Views:
    440
    Matthew Zimmer
    Dec 15, 2003
Loading...

Share This Page