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