我有一个每隔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;
}
}
答案 0 :(得分:1)
看起来最好为MeetingVO类添加一个属性,以跟踪您是否已经显示它。像public var hasBeenShown:Boolean = false
然后,在其他地方,如果您调用shouldShowNow,请将其更新为:
else if (!currMeeting.hasBeenShown && this.shouldShowNow(startTime_GMT, endTime_GMT, currMeeting))
然后在你的shouldshowNow函数中,将其更新为:
private function shouldShowNow(start:Date, end:Date, meeting:MeetingVO):Boolean
{
var now:Date = new Date;
var stime:Number = start.time - NOW_OFFSET_TIME;
var etime:Number = end.time;
if((now.time >= stime) && (now.time <= etime)){
meeting.hasBeenShown = true;
return true;
}
return false;
}