我正在闪存中创建一个记分板,正在从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();
答案 0 :(得分:0)
尽管我不确定您的代码到底有什么问题,但有些东西我不会在项目中使用:
请牢记以下所有内容:
// 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);
}