|
-
Apr 2nd, 2003, 11:11 AM
#1
Thread Starter
Fanatic Member
using "for" looping
hi there, the following scripts is for moving an object..
and now I want to make it loop 4 times...how can add a"for looping" to that script
-------this did not work---------
<script language="javascript">
for(var i=1;i<3;i++)
{
moveit()
}
</script>
-------------------
---------------
<html>
<head>
<style>
#piccy{color:green;background-color:yellow;width:80;height:80;}
body{background-color:black;color:yellow;}
</style>
<script>
var delay=10,ver=0, hor=0
function moveit()
{
if(hor<500)
{
piccy.style.left= hor;
hor=hor+5;
setTimeout('moveit()',delay);
}
if(hor==500){moveitd()}
}
function moveitd()
{
if(ver<330)
{
piccy.style.top= ver;
ver=ver+5;
setTimeout('moveitd()',(delay*10));
}
if(ver==330){moveitl()}
}
function moveitl()
{
if(hor>10)
{
piccy.style.left=hor;
hor=hor-5;
setTimeout('moveitl()',(delay+10));
}
if(hor==10){moveity()}
}
function moveity()
{
if(ver>0)
{
piccy.style.top=ver;
ver=ver-5;
setTimeout('moveity()',(delay+10));
}
}
</script>
</head>
<body>
<center>
<form>
<input type="button" value="start" onClick="moveit()">
<br>
</form>
<div id="piccy" style="position: absolute; top: 0px; left: 0px; width: 60px; height:60px;
visibility: visible">
<img src="cilek.gif" height="55" width="55">
</div>
<script language="javascript">
for(var i=1;i<3;i++)
{
moveit()
}
</script>
</body>
</html>
----------------------
thanks indeed
-
Apr 2nd, 2003, 02:32 PM
#2
Frenzied Member
Your for loop only executes when i = 1 and 2.
Change for(var i=1;i<3;i++) to for(var i=1;i<=4;i++)
-
Apr 8th, 2003, 11:43 PM
#3
Stuck in the 80s
Originally posted by ccoder
Your for loop only executes when i = 1 and 2.
Change for(var i=1;i<3;i++) to for(var i=1;i<=4;i++)
I believe this would be faster:
Code:
for(var i = 0; i < 4; i++)
Than using <=.
At least in C++, it is. But I'm sure speed isn't an issue with this. It's most likely unnoticable.
But my teacher always heckles us with speed issues since he works on coding airplane response units, and you want them to be as fast as can be.
Just thought I'd buzz in.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|