strange error.

Discussion in 'Javascript' started by ziobudda, Dec 28, 2006.

  1. ziobudda

    ziobudda Guest

    Hi all. I have this code:

    <html>
    <head>
    <script language="javascript" type="text/javascript">
    function sleep(n)
    {
    var now = new Date();
    var exitTime = now.getTime() + (n*1000);
    while (true) {
    now = new Date();
    if (now.getTime() > exitTime) return;
    }
    }

    function mostra() {
    tmp = document.createElement('input');
    tmp.setAttribute('type','text');
    tmp2= document.getElementById('miodiv');
    tmp2.appendChild(tmp);
    //Only for try
    tmp2.innerHTML = tmp2.innerHTML;
    sleep(1);
    }

    </script>
    </head>
    <body>
    <a href="#" onclick="mostra()">Clicca</a>
    <div id="miodiv" style=""></div>
    </body>
    </html>

    Ok ? I need to insert something into "miodiv" (an input type in this
    case) and stop javascript for 1 second.

    Now my problem: the "input type" is write into "miodiv" only after that
    the sleep() function is terminated.

    I don't know why.

    Tnx.

    PS: anyone have a sleep() function that works ?

    M.
    ziobudda, Dec 28, 2006
    #1
    1. Advertising

  2. ziobudda

    Jonas Raoni Guest

    ziobudda escreveu:
    > Hi all. I have this code:


    Falae Bunda xD

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


    The language attribute isn't needed ;]

    > function sleep(n)
    > {
    > var now = new Date();
    > var exitTime = now.getTime() + (n*1000);
    > while (true) {
    > now = new Date();
    > if (now.getTime() > exitTime) return;
    > }
    > }


    This is one of the worst things you can do in a language that is
    processed in the client :]

    > <a href="#" onclick="mostra()">Clicca</a>


    A phantom link, if it's not a link, use another element (e.g.: <span>) :]

    > Ok ? I need to insert something into "miodiv" (an input type in this
    > case) and stop javascript for 1 second.


    Why?

    You should use the setTimeout to achieve such delays.

    setTimeout(function(){
    alert("Then, after one second, pedala robinho xD");
    }, 1000);


    > Now my problem: the "input type" is write into "miodiv" only after that
    > the sleep() function is terminated.


    You made the browser sleep :)

    > PS: anyone have a sleep() function that works ?


    Your one works.


    --
    Jonas Raoni Soares Silva
    http://www.jsfromhell.com
    Jonas Raoni, Dec 28, 2006
    #2
    1. Advertising

  3. ziobudda

    yb Guest

    I'd recommend looking into window methods setTimeout / clearTimeout, or
    setInterval / clearInterval.

    A "sleep" function shouldn't be necessary. You don't want to "freeze"
    the browser like this.
    yb, Dec 28, 2006
    #3
  4. ziobudda

    Evertjan. Guest

    ziobudda wrote on 28 dec 2006 in comp.lang.javascript:

    > Hi all. I have this code:
    >
    > <html>
    > <head>
    > <script language="javascript" type="text/javascript">
    > function sleep(n)
    > {
    > var now = new Date();
    > var exitTime = now.getTime() + (n*1000);
    > while (true) {
    > now = new Date();
    > if (now.getTime() > exitTime) return;
    > }
    >}


    >
    > PS: anyone have a sleep() function that works ?


    If you had read up on a javascript sleep function, you would know it
    would not do to take over 100% of the allotted cpu time for such function
    in any multitasking environment.

    NEVER try a sleep function in zsuch environments, but use a kernetl
    timout function:

    var timeoutPointer = setTimeout('nextFunction()',milliseconds)

    This needs you to think in a modular programming concept,
    lot the old linear one from the single task assambler language, where a
    looping timeout was ok.

    > function mostra() {
    > tmp = document.createElement('input');
    > tmp.setAttribute('type','text');
    > tmp2= document.getElementById('miodiv');
    > tmp2.appendChild(tmp);
    > //Only for try
    > tmp2.innerHTML = tmp2.innerHTML;


    you mean something like:
    tmp2.innerHTML = tmp.value;
    ???

    > sleep(1);


    I have on idea why you should wait here,
    and how you could see that you waited!!!

    >}


    [please do not use tabs in usenet code texts]

    Try, subject to my understanding of what you would have wanted:

    ================ test.html ==========

    <script type='text/javascript'>

    function doIt() {
    var myDiv= document.getElementById('myDiv');
    var theInput = document.getElementById('theInput');
    myDiv.innerHTML = theInput.value;
    };

    function mostra() {
    var myDiv= document.getElementById('myDiv');
    myDiv.innerHTML = 'Please wait ......';
    setTimeout('doIt()',1200)
    }

    </script>

    <a href="#" onclick="mostra()">Clicca/click</a>
    <br><br>
    <input type='text' id='theInput' value='change this text'>
    <br><br>
    <div id="myDiv">-----------</div>

    ====================================



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 28, 2006
    #4
    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. Panic
    Replies:
    2
    Views:
    5,187
    Marc Guardiani
    Oct 24, 2003
  2. kwaj
    Replies:
    1
    Views:
    417
    Egbert Molenkamp
    Mar 7, 2004
  3. Jan
    Replies:
    2
    Views:
    1,419
    Mike Treseler
    Dec 16, 2004
  4. Harvey Twyman
    Replies:
    8
    Views:
    552
    August Derleth
    Oct 25, 2003
  5. Sam
    Replies:
    2
    Views:
    139
    Bob Barrows [MVP]
    Nov 16, 2007
Loading...

Share This Page