定时器、SetInterval、报警等

Posted

技术标签:

【中文标题】定时器、SetInterval、报警等【英文标题】:Timer, SetInterval, Alarm, etc 【发布时间】:2018-08-04 03:41:27 【问题描述】:

帮助将多个功能变成几个功能。

var myNumberOne = 10;
var myNumberTwo = 100;

函数一将创建一个每秒触发 2 次的计时器或间隔,跟踪单词“food”,完成后,“myNumberOne += 10”使 myNumberOne = 20;

函数二将创建一个计时器或间隔,每半秒触发 5 次,跟踪单词“ben”,完成后,“myNumberTwo += 50”使 myNumberTwo = 250;

对于两个函数来说这很好,但如果我有 100 种可能的组合,我认为应该这样做,没有间隔、计时器、函数等......相互干扰,并通过时间传递参数。

感谢您的帮助。

澄清:我想调用这样的函数

setTimeFunction("myTimeOne", myNumberOne, 2,1000,10, "ben");
setTimeFunction("myTimeTwo", myNumberTwo, 5,500,50,"food");

【问题讨论】:

【参考方案1】:

首先,您需要编写一个通用方法来执行许多类似的操作。跟踪很容易,但是您不能直接传递要更改的变量,因为您将传递一个值,而不是对变量的引用。为了做你想做的事,你需要将它作为一对“容器对象”和“变量名”传递以使用方括号表示法。

function myownDothings(target:Object, varname:String, adiff:int, totrace:String):void

    // Use square bracket notation to change the targeted variable.
    target[varname] += adiff;

    // Trace the given argument.
    trace(totrace);

好的,现在是 simple 复杂的部分。有一个 setTimeout(...) function 在给定超时的情况下多次调用给定方法,但官方文档官方建议使用 Timer class .

我希望你知道如何work with classes,因为你想要的东西调用 OOP 并将其安装到框架脚本中会导致一些丑陋的东西。因此,您需要编写一个类来记住要调用的函数、超时设置和一堆参数。

package

    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class Ticker
    
        // You need to keep the references to the things you use,
        // or else Garbage Collector might think you don't need it.
        static private var list:Array = new Array;

        // Instead of static method you can use the "constructor" way,
        // but I find it more stylish and it's one more thing for
        // you to google and learn of, which I totally approve.
        // The ... construction allows to pass a random number
        // of arguments (after fixed arguments) as an Array.
        static public function create(handler:Function, timeout:int, ...args:Array):void
        
            var aTicker:Ticker;

            // Brackets () are not mandatory with the "new" operator
            // if there are no mandatory constructor arguments.
            aTicker = new Ticker;

            // Store all the necessary data in the new instance. That's the 
            // point of OOP scripting here: you want to make 100 different 
            // tickers and you need each of them to keep some custom data.
            aTicker.timeout = timeout;
            aTicker.handler = handler;
            aTicker.args = args;

            // Finally, run the ticker.
            aTicker.start();

            // Store the created instance into the keeper list
            // to prevent Garbage Collector from destroying it.
            list.push(aTicker);
        

        // Again, fear the Garbage Collector.
        private var clock:Timer;

        // Keep in mind that timeout is not exactly accurate
        // as it aligns to the SWF's frame rate. Setting it up to call
        // more times a second than FPS will pose to be a meaningless act.
        private var timeout:int;

        // The reference to the method to call.
        private var handler:Function;

        // The list of arguments to pass to the method above.
        private var args:Array;

        // This method is called from the "create" method
        // to finalize things and start ticking.
        private function start():void
        
            // Create a Timer instance with a given timeout.
            clock = new Timer(timeout);

            // Subscribe the listener to the Timer.
            clock.addEventListener(TimerEvent.TIMER, onTick);

            // Start the Timer.
            clock.start();
       

        // The Timer instance will trigger this method
        // (approximately) every given timeout of milliseconds.
        private function onTick(e:TimerEvent):void
        
            // Now the idea is to call the given method
            // passing the list of given arguments to it.
            // Normally you don't need to pass the "this" object
            // to a method unless you use unnamed unbound closures.
            // (which I personally consider a heresy and don't recommend to use)
            // So you just pass "null" as the first argument and everything is fine.
            handler.apply(null, args);
        
    
 

现在,用法。这就是上面所有的恐怖最终闪耀的地方。

import Ticker;

var myNumberOne = 10;
var myNumberTwo = 100;

// Fire 2 times every second, increase "myNumberOne" by 10, trace the word "ben".
// So, 2 times a second it will call: myownDothings(this, "myNumberOne", 10, "ben");
Ticker.create(myownDothings, 1000 / 2, this, "myNumberOne", 10, "ben");

// Fire 5 times every half a second, increase "myNumberTwo" by 50, trace the word "food".
// So, 10 times a second it will call: myownDothings(this, "myNumberTwo", 50, "food");
Ticker.create(myownDothings, 500 / 5, this, "myNumberTwo", 50, "food");

【讨论】:

谢谢!我还没有实施(或尝试......)这个解决方案,但它看起来对我来说既实用、经济又具有教育意义。我对类和 OOP 有点熟悉,但这给了我一个深入研究这类事物的好地方。再次感谢代码中的 cmets。非常冗长。

以上是关于定时器、SetInterval、报警等的主要内容,如果未能解决你的问题,请参考以下文章

AS2.0 setInterval 和 clearInterval的用法

JS中setInterval()和clearInterval()的使用以及注意事项 (实用,赞)

JS定时任务

计数器不断重置为 setInterval 循环上的声明变量

vue中使用setInterval()循环定时器的注意事项

setInterval方法怎么了?