Results 1 to 5 of 5

Thread: using setTimeout

  1. #1

    Thread Starter
    Fanatic Member merhaba's Avatar
    Join Date
    Sep 2002
    Location
    Istanbul,Bartin-Gallipoli(Gelibolu-Canakkale)
    Posts
    601

    using setTimeout

    hi there,

    The following script is for moving an object(a textbox).but it does not move
    help me fixing the problem please.And is it possible to display
    "setTimeout value" as the script is running...!!

    thanks a lot
    *********************************
    <SCRIPT LANGUAGE="javascript">
    function runrace()
    {
    document.bgColor="red";
    document.fn.text1.style.left = document.fn.text1.style.left+=50;
    setTimeout("runrace()", 100);

    }
    </script>
    <form name=fn>
    <input type=button value="start" onclick="runrace()"><br>
    <INPUT TYPE=TEXT NAME="text1" SIZE="3">
    </form>
    Last edited by merhaba; Sep 7th, 2004 at 11:43 AM.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    there might also be something else but change "+=50" to "+50"
    Have I helped you? Please Rate my posts.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    style.left is often a string containing both value and unit. At least, it should be. As a result, +50 will append "50" to this string, leading to an invalid property value and thus no change.

    You have to parse the integer part out of style.left, hope that the unit is px, add your integer to the integer part and write the result out again.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    This kinda works. I didn't learn Javascript for long, so I don't know how to convert a string to a number. Try playing around with this:
    Code:
    <html>
    <head>
    <title>
    Test
    </title>
    <script language="javascript">
    var lastval = 0;
    function runrace()
    {
    var str = document.forms[0].elements[1].style.left;
    var start = str.indexOf("px");
    var val = str.substr(0, start);
    //alert(val);
    document.bgColor="red";
    lastval = val + 5;
    document.forms[0].elements[1].style.left = lastval;
    val = lastval
    alert('val now equals: ' + val);
    //alert(document.forms[0].elements[1].style.left+50);
    
    setTimeout("runrace()", 1000);
    }
    </script>
    </head>
    <body>
    <style type="text/css">
    .bla
    {
    cursor:hand;
    left: 500;
    }
    </style>
    <form name=fn>
    <input type=button value="start" onclick="runrace()"><br>
    <INPUT TYPE=TEXT NAME="text1" SIZE="3" style="position:absolute;">
    </form>
    </body>
    </html>
    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    you can use parseInt() to get the numbers. For instance:
    Code:
    var a = "123"
    alert(a+1)           //returns 1231
    alert(parseInt(a)+1) //return 124
    var b= "150px"
    alert(b)             //returns 150px
    alert(parseInt(b))   //returns 150
    alert(b+1)           //returns 150px1
    alert(parseInt(b)+1) //returns 151
    Have I helped you? Please Rate my posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width