auto refresh swf movie iusing action script 3

181 Views Asked by At

I am creating a score board in flash, am fetching data from REST endpoints. Am able to read data once properly , after that same data repeating not able to read the update data. I tried with timer , could any one help me here

Thanks in advance

my code below

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

There are 1 best solutions below

0
Organis On

Although I'm not sure what exactly is wrong with your code, there are things I wouldn't use in the project:

  1. Issuing HTTP requests to the same URL repeatedly on time basis, rather then waiting for each request to complete before sending another.
  2. Loader or URLLoader instances kept in local function variables. Normally, local function variables exist only as long as the function executes and their content (if not held by the application scope somehow) might be destroyed by Garbage Collector at literally ANY moment with no regard to your intent. UPD: There was an input that GC won't destroy an URLLoader while it has unfinished request to handle, still, having indestructible instances dangle somewhere you cannot reach them is not a good idea either.
  3. Functions defined inside other functions.

With all that in mind the following should work fine:

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