简体   繁体   English

流星setinterval太快了

[英]Meteor setinterval too fast

I have multiple countdown timers in my app and I'm updating those values using setInterval function. 我的应用程序中有多个倒计时器,我正在使用setInterval函数更新这些值。 The idea of following code block is the loop through all ongoing games and update countdown clocks. 遵循代码块的想法是循环所有正在进行的游戏并更新倒计时时钟。 I have 1000ms interval but it loops much faster than that. 我有1000毫秒的间隔,但它比这更快地循环。 Why? 为什么? When I open multiple browser windows to see countdown timer updates I can notice that interval is something like 500ms. 当我打开多个浏览器窗口以查看倒数计时器更新时,我可以注意到间隔是500毫秒。

if (Meteor.isClient) {

    Meteor.setInterval(
        function() {

            var going = Games.find({isGoing: true});
            var goingFetch = going.fetch();
            var goingCount = going.count();

            for (i = 0; i < goingCount; i++) {
                Games.update(goingFetch[i]._id, {$set: {timeLeft: goingFetch[i].timeLeft - 1}});
            }               
        }
    , 1000 );
}

Is there better way to solve this problem? 有没有更好的方法来解决这个问题? I may have 100 games simultaneously and every game have one to many timers. 我可能同时拥有100个游戏,每个游戏都有一个到多个计时器。 Timer type can be countdown or incremental and all timers are stoppable. 定时器类型可以是倒计时或增量,所有定时器都可以停止。 Interval is always and in all cases 1000ms. 间隔总是并且在所有情况下都是1000毫秒。

UPDATE I did more tests and setInterval works fine but clients still have too fast countdown timer. 更新我做了更多测试,setInterval工作正常,但客户端仍然有太快的倒计时器。

Are you sure your code isn't executed twice by mistake? 你确定你的代码没有被错误执行两次吗? Does goingCount produce correct results, for instance? 例如, goingCount是否goingCount产生正确的结果? setInterval isn't as accurate as setTimeout , but it shouldn't cause problems at 1000ms. setInterval不如setTimeout准确,但它不应该在1000ms时引起问题。 Can we see more of your code? 我们能看到更多你的代码吗?

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

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