javascript settimeout keeps going faster and faster?
<p>I am making a snake game in JavaScript, so if I press on an arrow key the 'snakehead' should keep moving in that direction.</p>

<p>The first time I press a key it's doing it nicely, but after that the head keeps moving faster and faster.</p>

<p>So what I want is something that moves the head once every 100 millisceconds. If you don't really get what i mean then have a look at this page: <a href="http://www.dbzweb.be/08/braetj08/snake.html" rel="nofollow">http://www.dbzweb.be/08/braetj08/snake.html</a></p>

<p>So, can someone tell me how to change my code so that it keeps running at same speed?</p>

<pre><code>function init_game() {
    if ((event ? event.keyCode : down) == (event ? 40 : 1)) /*down*/
    {
        if ((snakehead + z) &lt; z * z) {
            right = 0;
            left = 0;
            up = 0;
            down = 1;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gamebox";
            snakehead = snakehead + z;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gameboxsnake";
        } else {
            document.write("&lt;h1&gt;game over!&lt;/h1&gt; press this button to try again.&lt;br&gt;&lt;input     type='button' value='retry'     onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'&gt;");
        }
    }
    if ((event ? event.keyCode : up) == (event ? 38 : 1)) /*up*/
    {
        if ((snakehead - z) &gt; -1) {
            right = 0;
            left = 0;
            up = 1;
            down = 0;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gamebox";
            snakehead = snakehead - z;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gameboxsnake";
        } else {
            document.write("&lt;h1&gt;game over!&lt;/h1&gt; press this button to try again.&lt;br&gt;&lt;input    type='button' value='retry'    onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'&gt;");
        }
    }
    if ((event ? event.keyCode : right) == (event ? 39 : 1)) /*right*/
    {
        if ((snakehead + 1) % z !== 0) {
            right = 1;
            left = 0;
            up = 0;
            down = 0;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gamebox";
            snakehead = snakehead + 1;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gameboxsnake";
        } else {
            document.write("&lt;h1&gt;game over!&lt;/h1&gt; press this button to try again.&lt;br&gt;&lt;input     type='button' value='retry'   onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'&gt;");
        }
    }
    if ((event ? event.keyCode : left) == (event ? 37 : 1)) /*left*/
    {
        if (snakehead % z !== 0) {
            right = 0;
            left = 1;
            up = 0;
            down = 0;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gamebox";
            snakehead = snakehead - 1;
            document.getElementsByClassName(snakehead, "div", document)[0].id = "gameboxsnake";
        } else {
            document.write("&lt;h1&gt;game over!&lt;/h1&gt; press this button to try again.&lt;br&gt;&lt;input   type='button' value='retry' onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'&gt;");
        }
    }
    t = 0;
    t = setTimeout("init_game()", 100);
}
</code></pre>
