Javascript:重复功能不起作用
我编写这个函数是为了从右上到左下飞行DIV:Javascript:重复功能不起作用,javascript,Javascript,我编写这个函数是为了从右上到左下飞行DIV: var myObj; function infly() { myObj=document.getElementById('mydiv'); myObj.style.right='0px'; myObj.style.top='0px'; } function flyer() { var x=parseInt(myObj.style.right); var y=parseInt(myObj.style.to
var myObj;
function infly() {
myObj=document.getElementById('mydiv');
myObj.style.right='0px';
myObj.style.top='0px';
}
function flyer() {
var x=parseInt(myObj.style.right);
var y=parseInt(myObj.style.top);
x+=1;
y+=1;
myObj.style.right=x+'px';
myObj.style.top=y+'px';
}
function repeat()
{
setTimeout(flyer,5000)
}
HTML代码是:
<body onLoad="infly()">
<div id="mydiv">
</div>
<a href="" onClick="javascript:repeat()">Fly</a>
...
..
.
...
..
.
但是,重复
功能不起作用。当我在每次单击中删除此函数时,我的DIV将正确运行
我尝试使用setInterval('fly();',10)代码>但没有成功
谢谢你的帮助
更新:
我编辑代码并更正重复功能,但仍然不起作用。使用
setTimeout(fly,5000)
而不是
setTimeout("fly()","5000()")
使用
而不是
setTimeout("fly()","5000()")
setTimeout的第二个参数应该是一个数字:
setTimeout(fly,5000)
更新,最好不要使用“fly()”:
替换语法中的代码是要执行的代码字符串
延迟毫秒后。(不建议在以下情况下使用此语法:
与使用eval()的原因相同)
setTimeout的第二个参数应该是一个数字:
setTimeout(fly,5000)
更新,最好不要使用“fly()”:
替换语法中的代码是要执行的代码字符串
延迟毫秒后。(不建议在以下情况下使用此语法:
与使用eval()的原因相同)
将repeat函数更改为如下所示
function repeat() { setTimeout("fly()",5000) }
将repeat函数更改为如下所示
function repeat() { setTimeout("fly()",5000) }
如果希望函数不断移动而不重复单击,则需要调用repeat();在fly()的末尾也是。(如果是corse,则为您想要完成的任务…如果您希望函数在不重复单击的情况下不断移动,则需要调用repeat();在fly()的末尾也是。(如果是corse的话,你想完成什么…
setTimeout(“fly()”,5000)
setTimeout(“fly()”,5000)
下面是使div飞行的代码。我给它做了样式,这样你就可以看到它了。你的代码只调用了一次传单,如果没有将位置设置为绝对,div将不会重新显示
<script type="text/javascript">
var K_CYCLES = 20;
var numcycles = 0;
function infly() {
myObj = document.getElementById('mydiv');
myObj.style.right = '0px';
myObj.style.top = '0px';
}
function flyer() {
var x = parseInt(myObj.style.right);
var y = parseInt(myObj.style.top);
x += 10;
y += 10;
myObj.style.right = x + 'px';
myObj.style.top = y + 'px';
myObj.style.position = 'absolute';
numcycles++;
if (numcycles <= K_CYCLES)
{
setTimeout("flyer()",1000);
}
}
function repeat()
{
flyer();
}
</script>
</head>
<body onLoad="infly()">
<div id="mydiv" style="width:50px; height: 50px; border: 1px solid red;">
</div>
<input type="button" onClick="repeat()" value="fly"/>
</body>
var K_周期=20;
var numcycles=0;
函数infly(){
myObj=document.getElementById('mydiv');
myObj.style.right='0px';
myObj.style.top='0px';
}
函数flyer(){
var x=parseInt(myObj.style.right);
var y=parseInt(myObj.style.top);
x+=10;
y+=10;
myObj.style.right=x+'px';
myObj.style.top=y+'px';
myObj.style.position='绝对';
numcycles++;
if(numcycles这里是使div飞行的代码。我对其进行了样式设置,以便您可以看到它。您的代码只调用了flyer一次,如果不将位置设置为绝对值,div将不会重新显示
<script type="text/javascript">
var K_CYCLES = 20;
var numcycles = 0;
function infly() {
myObj = document.getElementById('mydiv');
myObj.style.right = '0px';
myObj.style.top = '0px';
}
function flyer() {
var x = parseInt(myObj.style.right);
var y = parseInt(myObj.style.top);
x += 10;
y += 10;
myObj.style.right = x + 'px';
myObj.style.top = y + 'px';
myObj.style.position = 'absolute';
numcycles++;
if (numcycles <= K_CYCLES)
{
setTimeout("flyer()",1000);
}
}
function repeat()
{
flyer();
}
</script>
</head>
<body onLoad="infly()">
<div id="mydiv" style="width:50px; height: 50px; border: 1px solid red;">
</div>
<input type="button" onClick="repeat()" value="fly"/>
</body>
var K_周期=20;
var numcycles=0;
函数infly(){
myObj=document.getElementById('mydiv');
myObj.style.right='0px';
myObj.style.top='0px';
}
函数flyer(){
var x=parseInt(myObj.style.right);
var y=parseInt(myObj.style.top);
x+=10;
y+=10;
myObj.style.right=x+'px';
myObj.style.top=y+'px';
myObj.style.position='绝对';
numcycles++;
如果(numcycles
@WillemMulder这是另一个优化。此答案提供了问题的答案,并包含相关文档的链接。因此+1。决不,决不,决不向setTimeout/setInterval传递字符串,否则需要对上下文进行评估。这肯定是一个错误的答案practice@RobW我并不反对mamoo的回答,但我的意思是为了改进它。eval是邪恶的。@WillemMulder这是另一个优化。这个答案提供了问题的答案,并包含了相关文档的链接。所以+1。永远,永远,永远,永远,永远不要将字符串传递给setTimeout/setInterval,否则需要对上下文进行评估。这肯定是个错误的答案practice@RobW我并不反对马穆的观点答案本身,但旨在改进。评估是邪恶的。做得好。但小问题。内部重复功能numcycles
应设置为0,以便循环重复。做得好。但小问题。内部重复功能numcycles
应设置为0,以便循环重复。