-
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>
-
there might also be something else but change "+=50" to "+50"
-
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.
-
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
-
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