<script> var x,y; function sample(){ var timer; var delay = 100; y=0; x=250; dx=25; var loop = function () { y=y+10; if(y>500)y=0; if(x>500)dx=-25; if(x<0)dx=25; x=x+dx; paint(); clearTimeout(timer); timer = setTimeout(loop, delay); } loop(); } function paint(){ var canv = document.getElementById('memo'); var ctx = canv.getContext('2d'); ctx.fillStyle = "white"; ctx.fillRect(0, 0, 500, 500); ctx.fillStyle = "red"; ctx.fillRect(x, y, 10, 10); } </script>