how do I call a javacript function every 60 seconds?

Posted by William on Stack Overflow See other posts from Stack Overflow or by William
Published on 2010-06-13T18:58:04Z Indexed on 2010/06/13 19:02 UTC
Read the original article Hit count: 264

Filed under:
|
|
|

So I'm trying to work on a Canvas demo, and I want this square to move from one side to the other, but I can't figure out how to call javascript in a way that repeats every 60 seconds.

Here's what I got so far:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Canvas test</title>
        <meta charset="utf-8" />
        <link href="/bms/style.css" rel="stylesheet" />

        <style>
            body { text-align: center; background-color: #000000;}
            canvas{ background-color: #ffffff;}
        </style>

        <script type="text/javascript">

        var x = 50;
        var y = 250;

        function update(){
            draw();
            x = x + 5;
        }

        function draw(){
          var canvas = document.getElementById('screen1');
          if (canvas.getContext){
            var ctx = canvas.getContext('2d');
            ctx.fillStyle = 'rgb(236,138,68)';
            ctx.fillRect(x,y,24,24); 
            }
        }
        </script>

    </head>

    <body onLoad="setTimeout(update(), 0);">
        <canvas id="screen1" width="500" height="500"></canvas>
    </body>
</html>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html