自动刷新swf电影iusing操作脚本3

时间:2018-11-24 00:37:17

标签: actionscript-3 flash flash-cs5

我正在闪存中创建一个记分板,正在从REST端点获取数据。能够一次正确读取数据,重复相同的数据后无法读取更新数据。我尝试过使用计时器,有人可以在这里帮助我

预先感谢

我的下面的代码

import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.display.Graphics;

    var white: Array = new Array();
    var black: Array = new Array();
    var red;
    var striker;

    var myTimer:Timer = new Timer(4000,100);
    myTimer.addEventListener(TimerEvent.TIMER, timerListener);
    function timerListener (e:TimerEvent):void{
    trace("Timer is Triggered");
        var urlLoader: URLLoader=null;

    // load the JSON data from the URL
    urlLoader = new URLLoader(new URLRequest("xxxxxxxxxxx"));
    urlLoader.addEventListener(Event.COMPLETE, loadData);

    // handle the load completion
    function loadData(e: Event):void {
        trace(e.target.data);

    }
    myTimer.start();
    stop();

1 个答案:

答案 0 :(得分:0)

尽管我不确定您的代码到底有什么问题,但有些东西我不会在项目中使用:

  1. 根据时间重复向相同的URL发送HTTP请求,而不是等待每个请求完成之后再发送另一个。
  2. 保存在本地函数变量中的
  3. Loader URLLoader 实例。通常,局部函数变量仅在函数执行时存在,并且它们的内容(如果不以某种方式保留在应用程序作用域之内)可能在真正的 ANY 时刻被Garbage Collector破坏,而与您的意图无关。 UPD:有人曾表示,GC在未完成要处理的请求的情况下不会破坏 URLLoader ,但仍然存在无法破坏的实例悬挂在您无法到达的地方,这并不是还是个好主意。
  4. 在其他函数中定义的函数。

请牢记以下所有内容:

// Keep URLLoader instance within the scope.
var L:URLLoader;

// Issue the first request.
loadNext();

// This function issues the new request.
function loadNext():void
{
    var aReq:URLRequest = new URLRequest("xxxxxxx");

    L = new URLLoader(aReq);
    L.addEventListener(Event.COMPLETE, onData);

    // Error handling - a must have.
    L.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
    L.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true);

    L.load();
}

// This function handles normal data processing.
function onData(e:Event):void
{
    // Sanity check. If event is triggered by anything
    // but the current URLLoader instance - do nothing.
    if (e.target != L) return;

    // Your data processing here.
    trace(L.data);

    // Clean-up the current request and schedule the next one.
    resetRequest();
    waitNext();
}

// This function handles all error events.
function onError(e:Event):void
{
    // Sanity check. If event is triggered by anyhting
    // but the current URLLoader instance - do nothing.
    if (e.target != L) return;

    // Report the error.
    trace("Oh, no:", e);

    // Clean-up the current request and schedule the next one.
    resetRequest();
    waitNext();
}

// This function destroys the current request.
function resetRequest():void
{
    // Sanity check. If there's no URLLoader instance - do nothing.
    if (!L) return;

    L.removeEventListener(Event.COMPLETE, onData);
    L = null;
}

// This function calls "loadNext" in 4 seconds.
function waitNext():void
{
    // Although the use of setTimeout counts as outdated,
    // it will still for our needs here.
    setTimeout(loadNext, 4000);
}