代码之家  ›  专栏  ›  技术社区  ›  vava

neko和haxe.timer.delayed()。

  •  3
  • vava  · 技术社区  · 17 年前

    正如每个haxe开发人员所知道的,您可以使用 haxe.Timer.delayed() 延迟函数调用一段时间。但是这个函数对于neko根本不存在。有没有办法达到同样的效果?

    4 回复  |  直到 17 年前
        1
  •  4
  •   vava    17 年前

    必须先检查一下,但是

    function delayed(f, time) {
       neko.vm.Thread.create(function() {
           neko.Sys.sleep(time);
           f();
       });
    }
    

    可能是最接近的。唯一的缺点是应用程序成为多线程的,这可能导致严重的问题。

        2
  •  1
  •   Tom    15 年前

    我考虑过你的问题,我认为最好的方法是为Neko创建自己的计时器类。我为你做了一个计时器课程:

    尼科蒂默

    package;
    import neko.Sys;
    
        class NekoTimer 
        {
        private static var threadActive:Bool = false;
        private static var timersList:Array<TimerInfo> = new Array<TimerInfo>();
        private static var timerInterval:Float = 0.1;
    
        public static function addTimer(interval:Int, callMethod:Void->Void):Int
        {
            //setup timer thread if not yet active
            if (!threadActive) setupTimerThread();
    
            //add the given timer
            return timersList.push(new TimerInfo(interval, callMethod, Sys.time() * 1000)) - 1;
        }
    
        public static function delTimer(id:Int):Void
        {
            timersList.splice(id, 1);
        }
    
        private static function setupTimerThread():Void
        {
            threadActive = true;
            neko.vm.Thread.create(function() {
                while (true) {
                    Sys.sleep(timerInterval);
                    for (timer in timersList) {
                        if (Sys.time() * 1000 - timer.lastCallTimestamp >= timer.interval) {
                            timer.callMethod();
                            timer.lastCallTimestamp = Sys.time() * 1000;
                        }
                    }
                }
            });
        }
    }
    
    private class TimerInfo
    {
        public var interval:Int;
        public var callMethod:Void->Void;
        public var lastCallTimestamp:Float;
    
        public function new(interval:Int, callMethod:Void->Void, lastCallTimestamp:Float) {
            this.interval = interval;
            this.callMethod = callMethod;
            this.lastCallTimestamp = lastCallTimestamp;
        }
    }
    

    这样称呼它:

    package ;
    
    import neko.Lib;
    
    class Main 
    {
        private var timerId:Int;
    
        public function new()
        {
            trace("setting up timer...");
            timerId = NekoTimer.addTimer(5000, timerCallback);
            trace(timerId);
    
            //idle main app
            while (true) { }
        }
    
        private function timerCallback():Void
        {
            trace("it's now 5 seconds later");
            NekoTimer.delTimer(timerId);
            trace("removed timer");
        }
    
        //neko constructor
        static function main() 
        {
            new Main();
        }
    }
    

    希望有帮助。

    注意:此项精度为100毫秒。您可以通过减少时间间隔设置来增加此精度。

        3
  •  1
  •   addiedx44    11 年前

    我也上过课,发现了一个问题。因为不是完全实时的,所以它会休眠间隔,调用函数,然后再次休眠间隔。因此,根据运行函数所需的时间,它的计时会变慢或变快。

    我把第39行改成这样解决了这个问题:

    //timer.lastCallTimestamp = Sys.time() * 1000;
    timer.lastCallTimestamp = timer.lastCallTimestamp + timer.interval;
    
        4
  •  0
  •   Michael Pliskin    17 年前

    是的,除了你在第一个回答中提到的以外,我什么都不知道。在Linux上,您可以使用sigalarm——但这看起来并不简单,100%纯C代码,需要非常小心地处理,以避免崩溃VM。

    推荐文章