<script> function sample(){ var timer; var delay = 100; p=0; var loop = function () { p=p+10; if(p>500)p=0; 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(250, p, 10, 10); } </script>