<script>
x=0
y=0
timeoutid=null;
function fly(){
if(x<400){
x++;
y++
clearTimeout(timeoutid)
self.moveTo(x,y)
timeoutid=setTimeout("fly()",20)
}else if(x>=400){ /*从这里开始,后面是水货,教我*/
x=400
y=400
clearTimeout(timeoutid)
self.moveTo(x--,y--)
timeoutid=setTimeout("fly()",10)
}
}
</script>
<body onload=fly()>
<body>
x=0
y=0
timeoutid=null;
function fly(){
if(x<400){
x++;
y++
clearTimeout(timeoutid)
self.moveTo(x,y)
timeoutid=setTimeout("fly()",20)
}else if(x>=400){ /*从这里开始,后面是水货,教我*/
x=400
y=400
clearTimeout(timeoutid)
self.moveTo(x--,y--)
timeoutid=setTimeout("fly()",10)
}
}
</script>
<body onload=fly()>
<body>
反复来回象这样:
<html>
<head>
<script>
x=0
y=0
var step=1;
var timeoutid;
function fly()
{
x+=step;y+=step
clearTimeout(timeoutid)
self.moveTo(x,y)
timeoutid=setTimeout("fly()",10)
if (x>400) step=-1
if (x<1) step=1
}
</script>
</head>
<body onload=fly()>
</body>
</html>一个来回的话,把setTimeout改成 if(x>0) setTimeout就成了
x=0
y=0
var step=1;
var timeoutid;
function fly()
{
x+=step;y+=step;
clearTimeout(timeoutid);
self.moveTo(x,y);
timeoutid=setTimeout("fly()",10);
if (x>400) step=-1;
if (x<1) step=1;
}
</script>
代码是对的,要控制循环次数就还要引入一个全局控制变量,比如
<script>
x=0
y=0
var step=1;
var timeoutid;
var count=0;
function fly()
{
if(count<10)//最多循环10次
{
x+=step;y+=step;
clearTimeout(timeoutid);
self.moveTo(x,y);
timeoutid=setTimeout("fly()",10);
if (x>400) step=-1;
if (x<1) {tep=1;count++;}
}
}
</script>
<script>
x=0
y=0
var step=1;
var timeoutid;
var count=0;
function fly()
{
if(count<10)//最多循环10次
{
x+=step;y+=step;
clearTimeout(timeoutid);
self.moveTo(x,y);
timeoutid=setTimeout("fly()",10);
if (x>400) step=-1;
if (x<1) {step=1;count++;}//上次这里把step 写为了tep
}
}
</script>