문제

모든 Haxe 개발자가 알고 있듯이 사용할 수 있습니다 haxe.Timer.delayed() 기능을 지연하려면 얼마 동안 호출하십시오. 그러나이 기능은 Neko에게 전혀 존재하지 않습니다. 동일한 결과를 달성 할 수있는 방법이 있습니까?

도움이 되었습니까?

해결책

먼저 확인해야하지만

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

가장 가까운 일일 수 있습니다. 유일한 단점은 응용 프로그램이 멀티 스레드가되어 심각한 문제를 일으킬 수 있다는 것입니다.

다른 팁

나는 당신의 문제에 대해 생각했고 가장 좋은 방법은 Neko를위한 자신의 타이머 클래스를 만드는 것입니다. 나는 당신을 위해 타이머 클래스를 만들었습니다.

Nekotimer.hx

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();
    }
}

도움이되기를 바랍니다.

참고 : 이것은 정확도가 100ms입니다. TimerInterval 설정을 줄임으로써이를 늘릴 수 있습니다.

나는 수업을 사용했고 하나의 문제를 발견했습니다. 완전히 실시간이 아니기 때문에 간격을 잠들고 기능을 호출하며 간격을 다시 잠을 잔다. 따라서 실행중인 기능의 시간에 따라 느리거나 빠르게 진드기가됩니다.

LINE 39를 그렇게 대체하여 해결했습니다.

//timer.lastCallTimestamp = Sys.time() * 1000;
timer.lastCallTimestamp = timer.lastCallTimestamp + timer.interval;

예, 첫 번째 답변에서 언급 한 것 외에는 아무것도 모릅니다. Linux에서는 Sigalarm을 사용할 수 있지만 이것은 사소한 100% 순수한 C 코드로 보이지 않으며 VM을 충돌하지 않도록주의를 기울여야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top