简体   繁体   English

Javascript Ninja Central Timer中的时间在哪里?

[英]Where's The Time in Javascript Ninja Central Timer?

The Central Timer Control presented in Secrets of the Javascript Ninja executes the added functions as quickly as possible. Java忍者的秘密”中介绍的中央计时器控件将尽快执行添加的功能。 How could I use this idea of Central Timer Control to have something happen every 250 ms? 我如何使用中央计时器控制的想法每250毫秒发生一次事情? What if I needed one thing to happen every 250 ms and another thing to happen every 600 ms? 如果我需要每250毫秒发生一件事而每600毫秒发生另一件事怎么办? I feel like I've really missed the boat here, somehow. 我觉得我真的很想念这里的船。

Sorry, I didn't have much time to write good readable code but if you run this script and walk it in the debugger you'll get an idea how to implement what you want in the single threaded environment. 抱歉,我没有太多时间来编写良好的可读代码,但是如果您运行此脚本并将其在调试器中运行,您将了解如何在单线程环境中实现所需的内容。 Please note that solution that uses Web Workers would probably work better if you want to make sure that you'll get one function shouting every 250ms and another 650ms, but that, I think, can't be called Central Timer Control anymore. 请注意,如果要确保每250ms和650ms分别喊出一个函数,则使用Web Workers的解决方案可能会更好地工作,但是我认为它不再称为Central Timer Control。

If you pass 300 to the delay function of the function 'one' you'll see why this method doesn't always work. 如果将300传递给函数“ one”的延迟功能,您将了解为什么此方法并不总是有效。

BTW Secrets of the Javascript Ninja is the best book about JavaScript that I've ever read. BTW Javascript的秘密Ninja是我读过的关于JavaScript的最好的书。 Good choice. 好的选择。

       var fns = [];

        function delay(ms) { // you can set delay manually to see what happens if functions take longer to execute
            ms += new Date().getTime();
            while (new Date() < ms){}
        } 


        var one = function(d) { // first function
            console.log(new Date() - temp);
            temp = new Date;
            delay(100); // let's say our function takes approximately 100 ms to execute
        }

        var two = function(d) { // second function
            console.log(new Date() - temp);
            temp = new Date;
            delay(150);

        }

        fns.push(one);
        fns.push(two);

        intervals = [250, 650];

        var temp = new Date;
        (function(fns, intervals) {
        var count = 0,
            start, finish, execTime; // helpers
        (function run () {
            start = new Date();
            fns[count](); // function executes here
            finish = new Date();
            execTime = finish - start; // time that function takes to execute
            setTimeout(run, ( (intervals[count] - execTime) > 0) ? intervals[count] - execTime : 0 ); // if function took too long to execute start as soon as you can, otherwise subtract execution time from the interval
            if (count === intervals.length - 1) {
                count = 0;
            } else {
                count++;
            }
        })()

        })(fns, intervals) // passing array of function to execute and array of intervals

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

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