试图在我的while循环中运行我的函数,并在其中移动方块的动画,我只希望它运行一次。不要让它工作。如果我使用setInterval,它只会动画多次。我现在怎么拥有动画呢?这是它现在的样子。欣赏一些提示。谢谢!运行函数一次(在一个while循环中)
编辑 - 正方形根据计数ID动画到不同的位置。
<?php
...
$count = 0;
while($rows = mysql_fetch_array($data)){ //many rows
$count = $count + 1
$id= $rows['id']; //different id's for each row
?>
<script>
var ID = '<?php echo $id; ?>';
var count = '<?php echo $count; ?>';
</script>
<div id="squares" style="height:50px; width:50px; position:relative; background:black;" >
<script>
document.getElementById('squares').id = ID; //make div id "squares" to ID
//So here the it doesn't work.
function(){
if(count == 1){
$('#' + ID).animate({left: (700), top: (300)}, 2000);
$('#' + ID).animate({left: (300), top: (500)}, 2000);
}
if(count == 2){
$('#' + ID).animate({left: (100), top: (400)}, 2000);
$('#' + ID).animate({left: (100), top: (800)}, 2000);
}
}
</script>
<?php
}
?>
“的setInterval” =>循环VS“setTimeout的” =>等待和运行一次 – pbenard
首先,你应该分开HTML,CSS, JavaScript和PHP。不要把所有东西放在同一个文件中,因为这样只会让你编码汤。 – str
如果你只想让它运行一次,为什么你要在循环中调用一个函数? – feeela