0

我有一个内置于 Flash cs5 中的 Adob​​e Air 桌面留言板应用程序,它在动态文本字段中加载一个外部“.txt”文件,并每 2 分钟检查一次新文件。我需要它来通知用户(NotificationType.CRITICAL),只有当文件是新的,而不是每次加载它时。是否可以?

应用程序中的所有代码:

NativeApplication.nativeApplication.startAtLogin=true

stage.nativeWindow.alwaysInFront=true;

//external text file load and recheck every 2 minutes

var myInterval:uint  = setInterval (loadUrl, 120000);
var loader:URLLoader = new URLLoader(new URLRequest("https://my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void {
  var loadedText:URLLoader = URLLoader(event.target);
if(myText_txt.htmlText!=loadedText.data){
   myText_txt.htmlText = loadedText.data;
     stage.nativeWindow.notifyUser(NotificationType.CRITICAL) 
}else {
  //do nothing
   }
}

function loadUrl():void {
    loader = new URLLoader(new URLRequest("https:///my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);
}

 // button control


Minimize_BTN.addEventListener(MouseEvent.CLICK, minimize);
function minimize(e:MouseEvent){
stage.nativeWindow.minimize();

}

drag_BTN.addEventListener(MouseEvent.MOUSE_DOWN, drag);
function drag(e:MouseEvent){
stage.nativeWindow.startMove();

}

stop(); //Stop on the frame you want
4

2 回答 2

0

您为什么不使用FileReference对象来了解一些属性,例如modifyDate并基于此验证文件是否不同,您还可以测试它的大小。

于 2013-01-16T07:56:27.513 回答
0

如果 myText_txt 字段正在修改加载的文本(例如,如果 myText_txt 的condenseWhite属性设置为 true),您的代码可能不起作用。确定文本是否已更改的更准确方法是将其存储在名为 oldText 的字符串变量中,然后oldText与新加载的文本进行比较。

这是您重新编写的部分代码以包含oldText变量。这段代码也更高效,因为它只实例化一些变量一次,并且避免了重复一些代码:

import flash.net.URLRequest;

function completeHandler(event:Event):void
{
    var newText:String = loader.data;
    if(newText != oldText)
    {
            myText_txt.htmlText = newText;
            stage.nativeWindow.notifyUser(NotificationType.CRITICAL);
            oldText = newText;
    }
}

function loadUrl():void
{
    loader.load(req);
}

// Initialize your variables and event handlers only one time
var req:URLRequest = new URLRequest("https:///my_text_file.txt");

// Set cacheResponse to false to prevent a successful response
// from being cached.
req.cacheResponse = false;

// And don't bother checking the cache, either. 
// Not necessary, but the request will execute a little faster.
req.useCache = false;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);

// Use oldText inside completeHandler() to determine 
// whether the file's text has changed
var oldText:String = "";

var myInterval:uint = setInterval(loadUrl, 120000);

// Start loading the text right away
loadUrl();
于 2013-01-16T08:38:46.157 回答