我有一个每 3 秒运行一次的间隔来检查新数据。如果它找到新数据,它会广播一个 dataChange 事件并且我的项目渲染器会更新。
我需要测试两次完全匹配时的比较......仅一次。意思是,数据回来是一个会议。会议于下午 3 点开始。比赛进行时,会议变为蓝色,用户可以进入。
我在比较时间匹配时广播了一个 dataChange 事件,但我不想一遍又一遍地广播它,即
private static const NOW_OFFSET_TIME:Number = 1000 * 60 * 15;
private function shouldShowNow(start:Date, end:Date):Boolean
{
var now:Date = new Date;
var stime:Number = start.time - NOW_OFFSET_TIME;
var etime:Number = end.time;
return ((now.time >= stime) && (now.time <= etime));
}
在上面的代码中,条件总是会返回一次 true
now.time >= stime
...并且 dataChange 事件一遍又一遍地运行。
但我的间隔只运行 3 秒,所以它不会捕获完全匹配。哎呀,我什至将我的间隔切换到每 500 毫秒,它不会捕获它。
我还有哪些其他选择?
感谢您提供任何有用的意见。
更新:我可以这样做(但我必须每秒运行一次间隔):
private static const NOW_OFFSET_TIME:Number = 1000 * 60 * 15;
private function shouldShowNow(start:Date, end:Date):Boolean
{
var now:Date = new Date;
var match:Boolean;
if( now.hours == start.hours && now.minutes == ( start.minutes - 15 ) && now.seconds == start.seconds ){
match = true;
}else{
match = false;
}
return match;
}
这是完整间隔的处理程序:
/*
We want to compare the current upcomingCalendarList collection
against the collection returned; if they are different, update the UI.
*/
var meetingsData:ArrayCollection = new ArrayCollection();
meetingsData = getArrayCollectionFromXML( event.result.response.participantMeetingList.meeting );
var cachedColl:ArrayCollection = com.fmr.transporter.model.GeneralInfoModel.getInstance().upcomingMeetingList;
var returnedColl:ArrayCollection = meetingsData;
var updates:Boolean = false; // our flag to let us know if there are changes in the meeting list
if( returnedColl != null )
{
// Meetings have been added/removed
if( cachedColl.length != returnedColl.length ){
updates = true;
}
// Look for meeting updates
else
{
for( var i:int=0;i<cachedColl.length;i++ ){
var currMeeting:MeetingVO = cachedColl[i] as MeetingVO;
for( var j:int=0;j<returnedColl.length;j++ ){
var returnedMtg:ObjectProxy = returnedColl[j];
/*
We want to ensure we're comparing the same meeting (meetingID) for
any changes.
*/
if( currMeeting.meetingID == returnedMtg.meetingId )
{
var startTime_GMT:Date = converServerUTCTimeStampToLocalDate( returnedMtg.startTime );
var endTime_GMT:Date = converServerUTCTimeStampToLocalDate( returnedMtg.endTime );
if( ObjectUtil.dateCompare( currMeeting.startTime, startTime_GMT ) != 0 )
updates = true;
else if( ObjectUtil.dateCompare( currMeeting.endTime, endTime_GMT ) != 0 )
updates = true;
else if( currMeeting.meetingName != returnedMtg.meetingName )
updates = true;
else if( this.shouldShowNow( startTime_GMT, endTime_GMT ) )
updates = true;
}
}
}
}
// If there are no updates, leave the cached collections alone.
if( !updates ){
return;
}
}