簡體   English   中英

Javascript SetInterval計時問題,總時間

[英]Javascript SetInterval timing issue, total time

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

  base.time ++;                      

},1);

不應該move(); 功能每9s發生一次? 我給它計時,它的時間要少得多,為什么呢?

setInterval不會每毫秒運行一次。 最小可能的間隔大於此間隔。

如果您希望某件事在9秒鍾內運行,則應使用setTimeout() 9秒鍾。 另外,您的代碼不會將base.time重置為零,因此無論如何它只會匹配9000。

如果希望它每9秒運行一次,則可以使用setInterval(handler, 9000)或使用setTimeout(handler, 9000) ,然后在處理程序函數中設置下一個setTimeout

這將每九秒執行一次move()

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

這是有關該主題的有用文章: http : //www.adequatelygoodgood.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/

間隔很容易做到!

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

setInterval(move, 9000);

看到它在jsFiddle上工作

您不能指望setInterval實際上每1毫秒運行一次。 如果將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.

您在評論中寫道,有一個按鈕可以重置時間,這就是為什么您不想只setTimeout來獲得全部延遲。 解決方法如下:

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

每次調用start()時,時間將從現在開始重置為9秒,如果經過9秒,將調用move()並開始另一個9秒的間隔。 如果您實際上不希望它重復發生,只需使用setTimeout即可。

關鍵是使用clearInterval(或clearTimeout)來取消之前的9秒延遲並開始一個新的延遲。 用垃圾值調用clearInterval是無害的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM