简体   繁体   English

JavaScript setTimeout运行两次

[英]JavaScript setTimeout Runs Twice

I am trying to make a function that starts in exact intervals to keep stanble update rate. 我正在尝试使函数以确切的间隔启动,以保持稳定的更新率。 The problem is that it seems to execute in 2 channels. 问题在于它似乎在2个通道中执行。 This is the log: 这是日志:

timeElapsed=141; lastDraw=1314040922291
timeElapsed=860; lastDraw=1314040923151
timeElapsed=141; lastDraw=1314040923292
timeElapsed=860; lastDraw=1314040924152
timeElapsed=141; lastDraw=1314040924293
timeElapsed=860; lastDraw=1314040925153
timeElapsed=141; lastDraw=1314040925294
timeElapsed=860; lastDraw=1314040926154
timeElapsed=141; lastDraw=1314040926295
timeElapsed=859; lastDraw=1314040927154
timeElapsed=143; lastDraw=1314040927297
timeElapsed=858; lastDraw=1314040928155
timeElapsed=143; lastDraw=1314040928298
timeElapsed=858; lastDraw=1314040929156
timeElapsed=142; lastDraw=1314040929298

First, I exectute my function using 首先,我使用

drawTimer = setTimeout(function(){ draw() }, 1);

and the function looks like this: 函数看起来像这样:

var draw = function(){
    if(!running)
        return;

    var miliseconds = getCurrentMiliseconds();
    var timeElapsed = miliseconds - lastDraw;
    lastDraw = miliseconds;

    console.log("timeElapsed=" + timeElapsed + "; lastDraw=" + lastDraw);
    onDrawListener(timeElapsed);

    if(timeElapsed < timeLapse)
        miliseconds = timeLapse - timeElapsed;
    else
        miliseconds = 1;        

    drawTimer = setTimeout(function(){ draw() }, miliseconds);
}

It happens in both, Chrome and Firefox. Chrome和Firefox均会发生这种情况。 Do you know what is it caused by? 你知道是什么原因造成的吗? And... How to fix it? 还有...如何解决?

PS Since everyone seems to be so confused about the running variable, here it is: it's a private parent object member that indicates whether the mechanism is still running or has stopped. PS由于每个人似乎都对运行变量感到困惑,因此,它是:它是一个私有父对象成员,它指示该机制是仍在运行还是已停止。 It's set by other functions and is just there to make sure this function doesn't continue working after stop() is called. 它是由其他函数设置的,只是用来确保在调用stop()之后此函数不会继续工作。

-- update -- timeLapse is set to 1000 (1 time per seconds) and never changed again. -update-timeLapse设置为1000(每秒1次),并且不再更改。

onDrawListener is set to this function: onDrawListener设置为此功能:

function(timeElapsed){
        canvas.clear();

        moveSnake();

        if(snake.body[0] == food){
            food = getRandomFreePosition();
            ++snake.body.lenght;
        }


        drawBackground();
        drawFood();
        drawSnake();

    }

to explain it: canvas is kinda the engine that takes care of callbacks, key listening and also has a few functions. 解释一下:canvas有点像是负责回调,键侦听的引擎,并且还具有一些功能。 Other than that seems kinda self-explaining. 除此之外,这似乎是不言自明的。 they do nothing other than some int algorithms and drawing in the canvas. 他们除了做一些int算法和在画布上绘制外什么都不做。

-- Figured out -- I understood that I should calculate time spent for current function and not since the last one started. -弄清楚了-我理解我应该计算当前功能所花费的时间,而不是自上一个功能开始以来。 My old method worked not in 2 channels but rather in long-short-long-short-long-... delayes 我的旧方法不是在2个频道中工作,而是在long-short-long-short-long -...延迟中

首先,您不设置运行中的布尔值,而且当您输入函数时,请立即在drawTimer上执行clearTimeout。

clearTimeout(drawTimer);

In a loop like that, you should consider to write: 在这样的循环中,您应该考虑编写:

 if(timeElapsed >= AMOUNT_OF_TIME) 
{   
 // run code 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM