javascript settimeout继续越来越快?

时间:2012-05-16 20:32:36

标签: javascript

我正在使用JavaScript进行蛇形游戏,所以如果我按下箭头键,“蛇头”应该继续向那个方向移动。

我第一次按下按键时,它正在做得很好,但在此之后,磁头的移动速度越来越快。

所以我想要的是每100毫秒移动一次头部的东西。如果您没有真正理解我的意思,请查看此页面:http://www.dbzweb.be/08/braetj08/snake.html

那么,有人可以告诉我如何更改我的代码以使其以相同的速度运行吗?

function init_game() {
    if ((event ? event.keyCode : down) == (event ? 40 : 1)) /*down*/
    {
        if ((snakehead + z) < 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("<h1>game over!</h1> press this button to try again.<br><input     type='button' value='retry'     onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'>");
        }
    }
    if ((event ? event.keyCode : up) == (event ? 38 : 1)) /*up*/
    {
        if ((snakehead - z) > -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("<h1>game over!</h1> press this button to try again.<br><input    type='button' value='retry'    onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'>");
        }
    }
    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("<h1>game over!</h1> press this button to try again.<br><input     type='button' value='retry'   onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'>");
        }
    }
    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("<h1>game over!</h1> press this button to try again.<br><input   type='button' value='retry' onclick='window.open(\"http://www.dbzweb.be/08/braetj08/snake.html\",\"_self\")'>");
        }
    }
    t = 0;
    t = setTimeout("init_game()", 100);
}

1 个答案:

答案 0 :(得分:3)

t = 0;
t = setTimeout("init_game()", 100);

这不符合你的想法。这会将变量t设置为0(此清除超时),然后立即将其设置为timeoutID返回的setTimeout

蛇正在加速,因为每按一次键init_game再次运行,从而增加了再次运行所有呼叫init_game的超时时间。

尝试将t = 0;更改为clearTimeout(t)。不过,您需要在代码之前声明t(将var t;添加到顶部)。

另外,请勿将字符串传递给setTimeout,而是使用eval。传递函数。

clearTimeout(t);
t = setTimeout(init_game, 100);

注意:在document.write("<h1>game over!</h1>之后,您应该添加return;以阻止init_game循环(并打印无限的“游戏结束!”消息)。

DEMO:http://jsfiddle.net/T5kPg/1/