Javascript SetInterval计时问题,总时间

时间:2011-08-03 22:55:09

标签: javascript events

base ={time:0};    
var loop = 0;
 setInterval(function(){
    if(base.time === 9000){
        move();
       base.time = 0;
     }

  base.time ++;                      

},1);

不应该移动();功能每9s发生一次?我把它计时了,更不用了,为什么会这样?

4 个答案:

答案 0 :(得分:1)

setInterval不会每毫秒运行一次。有一个最小可能的间隔比这长。

如果您希望在9秒内运行某些内容,则应使用setTimeout() 9秒。另外,您的代码不会将base.time重置为零,因此无论如何它只会匹配9000。

如果您希望它每9秒运行一次,那么您可以使用setInterval(handler, 9000),或者您可以使用setTimeout(handler, 9000),然后在处理程序函数中设置下一个setTimeout

这将每九秒执行一次move()

var intervalTimer = setInterval(function(){
    move();
}, 9000);

以下是有关该主题的有用文章:http://www.adequatelygood.com/2010/2/Minimum-Timer-Intervals-in-JavaScript

要在单击按钮时将时间重置为9秒,请使用以下代码:

var intervalTimer;

function startTimer() {
    intervalTimer = setInterval(function(){
        move();
    }, 9000);
}

function handleClick() {
    clearInterval(intervalTimer);   // stop currently running interval
    startTimer();
}

startTimer();

在此处查看此行动:http://jsfiddle.net/jfriend00/sF2by/

答案 1 :(得分:0)

间隔很容易就像馅饼一样!

var move = function(){
    alert("move!");
};

setInterval(move, 9000);

See it work here on jsFiddle

答案 2 :(得分:0)

你不能指望实际每1毫秒运行setInterval。如果CPU用于其他进程,则可能不会运行1 。而是使用以下之一:

function move() {
    // Do stuff.
}

// The obvious solution.
// Certain browsers (Chrome) may put the script in "inactive" mode which will
// pause setInterval code. This means move will be run too few times, if you
// actually depend on it being called X times for Y time.
setInterval(move, 9000);

// The other solution.
// Get the delta between each loop and run the move loop as necessary.
// WARNING: This is not efficient, and you should only use this if you have a
//          good reason to do so.
// EXTRA WARNING: This code is actually retarded in its current form. It's just
//                here to show you how you'd do it. Since you didn't post your
//                original problem, it's hard to know what you're really after.
var time = +new Date, frequency = 9000;
setInterval(function () {
    var dt = new Date - time;
    // Check if we've waited long enough.
    if (dt >= frequency) {
        // If the process hangs for 20 seconds, this value would be 2. Usually,
        // it will be 1.
        // Also, Chrome will pause interval counters, so if a tab is inactive,
        // this count could be really high when the tab gets focus again.
        var times = Math.floor(dt / frequency);
        console.log('Moving', times, 'time(s)!');
        for (var i = 0; i < times; i++) {
            move();
        }
        // Start counting time from the last update.
        time += times * frequency;
    }
}, 1); // 1 could probably be much higher here. Depends on your use case.

答案 3 :(得分:0)

你在评论中写道,有一个按钮可以重置时间,这就是为什么你不想只是设置完全延迟的时间。以下是如何处理:

var running;
function start() {
    clearInterval(running);
    running = clearInterval(function () {
        move();
    }, 9000);
}

每次调用start()时,时间将从现在开始重置为9秒,如果经过9秒,将调用move()并开始另一个9秒间隔。如果你实际上并不希望它反复发生,那就改用setTimeout。

关键是使用clearInterval(或clearTimeout)取消之前的9秒延迟并启动一个新延迟。使用垃圾值调用clearInterval是无害的。